How to write a mixed data (text and number) in a CSV file ?

조회 수: 135 (최근 30일)
Raguram N
Raguram N 2017년 6월 23일
댓글: dpb 2021년 10월 10일
I need to write a CSV file with text matrix in the first column and numbers matrix in the second column.
Example
matrix1 = {'water';'space';'fire'};
matrix2 = [100;200;300];
CSV file output as:
water,100
space,200
fire,300
Please provide me some direction to solve this problem. Please let me know for further information.
Thanks in advance, Ram

답변 (4개)

per isakson
per isakson 2017년 6월 23일
편집: per isakson 2017년 6월 23일
matrix1 = {'water';'space';'fire'};
matrix2 = [100;200;300];
fid = fopen( 'matrix.csv', 'w' );
for jj = 1 : length( matrix1 )
fprintf( fid, '%s,%d\n', matrix1{jj}, matrix2(jj) );
end
fclose( fid );
The format specifier %d assumes that matrix2 contains whole numbers, if not use %f

dpb
dpb 2017년 6월 23일
It seems like "'round Robin Hood's barn!" way to get there, but believe it or not,
fname='yourfile.csv';
writetable(cell2table([matrix1 num2cell(matrix2),fname,'writevariablenames',0)
is about as simple a way to do the job as there is.
The mid-level routines such as dlmwrite are simply unable to deal with the cellstr content while the low-level routine fprintf requires you to handle all the formatting and loop writing each record one-at-a-time to build the file. Not terribly difficult but tedious at best and seems much harder than it actually is when just getting started. TMW has done quite a bit on the input side; the output side is lagging although writetable is a start, Matlab needs better high-level tools for outputting data that is something other than just double arrays.
  댓글 수: 2
Daelyn Greene
Daelyn Greene 2019년 2월 20일
편집: Daelyn Greene 2019년 2월 20일
Is there a way to append new data to the end of an existing file using this? I am wanting to do something exactly like this, only with data being gradually input over time from selections in a UI.
dpb
dpb 2019년 2월 21일
With this workaround, no; not directly to text file; writetable has no "append" option.
You could take the additional circuitious route of writing to a spreadsheet and then saving it, but that would require keeping track of the number of lines written to compute the right range to specify so, while possible, not of more than theoretical interest.
In that case, there are any number of posts/answers but Per's solution below is the trick except you must open the file with the 'append' flag of 'a' or 'a+' instead of 'w' to not overwrite existing data in the file.

댓글을 달려면 로그인하십시오.


breathi
breathi 2019년 12월 13일
Since R2019a,
writecell
is doing this perfectly for a cell array.

Mohit Rajora
Mohit Rajora 2020년 8월 19일
To write the data in C to a CSV file. Use “writetable” in combination with the “cell2table” function.
  댓글 수: 2
Mohamed Abdelhamid
Mohamed Abdelhamid 2021년 10월 10일
Is there away to avoid the "varN" added to the written file when using "writetable"?
dpb
dpb 2021년 10월 10일
See the doc for optional inputs...
...,'WriteVariableNames',0);
Or, as the other respondent above noted, use writecell instead. (Altho it calls writetable after using cell2table internally)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by