필터 지우기
필터 지우기

How to write 2x1 string to csv?

조회 수: 14 (최근 30일)
Louise Wilson
Louise Wilson 2019년 9월 4일
댓글: Walter Roberson 2019년 9월 6일
I have created a 2x1 string array which contains filenames in the first column, and a 'yes' or 'no' in the second column.
I'd like to write this information to an output format, preferrable .csv, how could I do this? Should I be converting from string to another format first?
Thanks!! My work so far:
%Create output array, table with two columns
sz1=files; %number of rows=number files in folder
szN=2; %number of columns=2
results=strings(sz1,szN);
d(i).name=convertCharsToStrings(d(i).name); %convert filename to suitable format
%Does it look like there is boat noise in the plot?
answer=input('Can you see boat noise? y/n: \n', 's')
if strcmp(answer,'y');
%store filename in output with 'boat' in second column
results(row,1) = d(i).name;
results(row,2) = 'yesboat';
elseif strcmp(answer,'n');
%store filename in output with 'no boat' in second column
results(row,1) = d(i).name;
results(row,2) = 'noboat';
else
disp('Input must be Y or N!');
end

채택된 답변

Walter Roberson
Walter Roberson 2019년 9월 4일
편집: Walter Roberson 2019년 9월 4일
You can convert to a table() object and writetable() with WriteVariableNames set to false.
Or you can do it "manually", by using fopen(), fprintf(), fclose()
Or you can
char(results(:,1) + ',' + results(:,2))
and dlmwrite() that to a .csv file, making sure you use 'delimiter', '' (that is the empty character vector)
If you were using R2019a or later (and thank you for filling in your release!), then you could use writematrix()
  댓글 수: 2
Louise Wilson
Louise Wilson 2019년 9월 6일
Thanks Walter. I had been wanting to use writematrix as I see dlmwrite is no longer recommended nor is writetable. I downloaded a function instead called cell2csv, I think it was called... that someone wrote to do this. It worked perfectly. I don't really understand the steps you said but maybe I will in the future, I'll refer back. Thanks!
Walter Roberson
Walter Roberson 2019년 9월 6일
writematrix() needs the release after you have.
writetable() is fine for tables of data that is not all the same datatype.

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by