MATLAB Answers

writing cell array to file

1 view (last 30 days)
I have a large text file with entries/lines that look like as follows.
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)};
The output file comes out with the columns of the input file transposed. This is what I see in the output file.
Thanks for any tips.
Amjad Luna
Amjad Luna on 28 Feb 2020
Tried already. Gives me

Sign in to comment.

Accepted Answer

Sravya Pulavarthi
Sravya Pulavarthi on 12 Feb 2021
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);

More Answers (0)




Community Treasure Hunt

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

Start Hunting!