write multiple line of text into a text file
Show older comments
Simple question; I have this variable which I want easily write to a text file (it can be a hundred of lines, I just cut it into 4 to fit here)
app.DCMtxt.Value
ans =
4×1 cell array
'3 Message(s) transmitted '
''
' ws BUS ID Rx Node DLC Cycle Extended'
' ______ ___ ____ __ ____ ___ _____ ________'
4 Comments
per isakson
on 3 Aug 2017
Edited: per isakson
on 3 Aug 2017
"easily write to a text file" Does this qualify as easy?
len = size( app.DCMtxt.Value, 1 );
fid = fopen('output.txt','w');
for jj = 1 : len
fprintf( fid, '%\s\n', app.DCMtxt.Value{jj,1} );
end
fclose(fid);
Rik
on 4 Aug 2017
Of course, you should make sure your cell is indeed (n x 1). For other cases you need to think what you want to do with the extra cols.
Ali
on 4 Aug 2017
Accepted Answer
More Answers (1)
Will Wilson
on 4 Aug 2017
If you didn't want to use a for loop directly you could use a newer datatype called a table and one if it's helper functions to get the job done. Here is an example:
% Convert your [m x 1] cell array into a MATLAB table.
data = cell2table(app.DCMtxt.Value);
% (Optionally) name the column in your table.
data.Properties.VariableNames = {'DataSet'};
% Write the table out to a text file.
writetable(data,'OutputData');
This should do the trick. Ultimately it will depend on how much control you need on the text file you write out.
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!