How can we write data into csv file with column labels included ?

196 views (last 30 days)
I want to write the data into csv file with column labels. I have illustrated the code below.
chead = {'a','b','c','d','e','f','g','h','i','k','l'};
data1 = ones(1001,4);
data2 = ones(125,7);
data = {data1,data2};
R = [chead;data];
writecell(R,'file.csv');
But this doesn't work the way i expected. This was writing the data like:
a b c .......
1 1 1 1 .... 1 1 1 1 .... 1 1 1 1....
instead of
a b c .......
1 1 1
1 1 1
1 1 1
1 1 1
. . .
. . .
If anyone knows please tell the answer.
  2 Comments
Mahesh Babu Dhanekula
Mahesh Babu Dhanekula on 22 Aug 2020
It doesn't support vectors having variable row length. Can you tell me any workaround method for using this command ?

Sign in to comment.

Answers (2)

dpb
dpb on 22 Aug 2020
Edited: dpb on 22 Aug 2020
...
data1 = ones(1001,4);
data2 = ones(125,7);
data = {data1,data2};
...
You have created a disparate-sized array of 125 rows of seven variables and the remainder with only four -- this can't be put into a rectangular array to write a regular .csv file. writecell did what you asked it to do, output the content of the cell array you built; you just didn't build a representation of what you (apparently) intended.
What do you intend the content of the file to be for the shorter records -- missing values or shorter records?
I've not tested if writecell will build a file -- well, let's just see:
>> writecell(cell(2,4))
>> type cell.txt
,,,
,,,
>>
Ah! Indeed it will, if you convert your numeric array by num2cell to a cell array of one element to cell, and add the empty cells where needed, joy wil ensue.
ADDENDUM:
While it's relatively trivial to write using fprintf, you can try something like
names={'a','b','c'}; % build a set of variable names
writecell(names) % write to names.txt (pick your name as desired)
c= [ones(2,3) zeros(2,4)]; % longer rows
save names.txt c -ascii -append % add to existing file
c= ones(2,3); % short rows
save names.txt c -ascii -append % ditto
results in
>> type names.txt
a,b,c
1.0000000e+00 1.0000000e+00 1.0000000e+00 0.0000000e+00 0.0000000e+00 0.0000000e+00 0.0000000e+00
1.0000000e+00 1.0000000e+00 1.0000000e+00 0.0000000e+00 0.0000000e+00 0.0000000e+00 0.0000000e+00
1.0000000e+00 1.0000000e+00 1.0000000e+00
1.0000000e+00 1.0000000e+00 1.0000000e+00
>>
  2 Comments
Mahesh Babu Dhanekula
Mahesh Babu Dhanekula on 22 Aug 2020
but i am not interested in having additional zeros in columns. so i just wanted this to work as i expected.
dpb
dpb on 22 Aug 2020
You'll have to write the file specifically with low-level calls, then. There is no prepared function to write an irregular file of that format.
You'll run into trouble trying to read the file back again, anyway, unless an application is aware of this issue going in.

Sign in to comment.


giancarlo maldonado cardenas
you can do it with this
A=[4 5 6;7 8 9]
T = array2table(A)
T.Properties.VariableNames(1:3) = {'x_axis','y_axis','z_axis'}
writetable(T,'file1.csv')

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!