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

232 ビュー (過去 30 日間)
Mahesh Babu Dhanekula
Mahesh Babu Dhanekula 2020 年 8 月 22 日
コメント済み: dpb 2022 年 6 月 6 日
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 件のコメント
KSSV
KSSV 2020 年 8 月 22 日
REad about writetable.
Mahesh Babu Dhanekula
Mahesh Babu Dhanekula 2020 年 8 月 22 日
It doesn't support vectors having variable row length. Can you tell me any workaround method for using this command ?

サインインしてコメントする。

回答 (2 件)

dpb
dpb 2020 年 8 月 22 日
編集済み: dpb 2020 年 8 月 22 日
...
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 件のコメント
Mahesh Babu Dhanekula
Mahesh Babu Dhanekula 2020 年 8 月 22 日
but i am not interested in having additional zeros in columns. so i just wanted this to work as i expected.
dpb
dpb 2020 年 8 月 22 日
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.

サインインしてコメントする。


giancarlo maldonado cardenas
giancarlo maldonado cardenas 2022 年 6 月 6 日
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')
  1 件のコメント
dpb
dpb 2022 年 6 月 6 日
That handles only regular arrays; not the variable-sized cells of the original Q?.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by