writing cell array to file

조회 수: 1 (최근 30일)
Amjad Luna
Amjad Luna 2020년 2월 28일
답변: Sravya Pulavarthi 2021년 2월 12일
Hi,
I have a large text file with entries/lines that look like as follows.
-60,-69,""
-63,-77,""
-64,-79,""
-69,-85,""
-75,-91,""
-75,-91,""
-79,-77,""
I need to extract the first N (say three) lines and write them to another text file. I am doing the following.
fp_in=fopen(input_file_name, 'r');
inDump=textscan(fp_in, '%s%s%s', 'Delimiter', ',');
outDump={inDump{1}(1:3) inDump{2}(1:3) inDump{3}(1:3)};
T=cell2table(outDump);
writetable(T,output_file_name);
The output file comes out with the columns of the input file transposed. This is what I see in the output file.
-60,-63,-64,-69,-77,-79,"","",""
Thanks for any tips.
  댓글 수: 2
Walter Roberson
Walter Roberson 2020년 2월 28일
outDump={inDump{1}(1:3); inDump{2}(1:3); inDump{3}(1:3)};
Amjad Luna
Amjad Luna 2020년 2월 28일
Tried already. Gives me
-60,-63,-64
-69,-77,-79
"","","","",""

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

채택된 답변

Sravya Pulavarthi
Sravya Pulavarthi 2021년 2월 12일
Hi Amjad, you can use readtable and writetable to do this:
>> r = readtable(input_file_name);
>> N = 3; % Since you want to extract the first 3 lines of your file
>> writetable(r(1:N,:), output_file_name, "QuoteStrings", true, "WriteVariableNames", false);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by