string to text file
12 views (last 30 days)
Show older comments
Matthew Tyler Jeffries
on 8 Mar 2019
Edited: per isakson
on 9 Mar 2019
Hello,
I have a program that produces a very large string array, and I would like to be able to open it in Notepad++. I tried going into Notepad++ to open my file, but when it opened, it looked like gibberish. How can I convert my matlab string array so I can open it in Notepad++?
Thank you
8 Comments
Accepted Answer
per isakson
on 8 Mar 2019
Edited: per isakson
on 8 Mar 2019
This creates a textfile, which notepadd++ opens.
v1=str(:,1);
v2=str(:,2);
v3=str(:,3);
T = table( v1,v2,v3 );
writetable( T, 'str.csv' )
as does
T = table( str(:,1),str(:,2),str(:,3) );
writetable( T, 'str.csv' )
however, this throws an error
>> writetable( table( str ), 'str.csv' )
Error using writetable (line 142)
Unable to perform assignment because the left and right sides have a different number of
elements.
Better control of the format
%%
cac = str2cell( str );
fid = fopen( 'h:\m\cssm\str.csv', 'w' );
fprintf( fid, '%-24s %-24s %-24s\n', cac{:} );
fclose( fid );
Where (OCR and cleaning)
%%
str = [
"later ite-DD030" "210801" "210801"
"later ite-DD036" "28160" "28160"
"later ite-DDU7" "127198" "127198"
"later ite-DDC4g" "204186" "204186"
"laterite-DDOSO" "19161" "19161"
"later ite-DDD63" "302210" "302210"
"later ite-DDD68" "258159" "214865"
"later ite-DDDEg" "214865" "231473"
"laterite-DD073" "231473" "203491"
"later ite-DD074" "210801" "210801"
"laterite-DDD81" "28160" "28160"
"later ite-DDDB8" "127198" "127198"
"laterite-DOID8" "204186" "204186"
"laterite-DOIDg" "19161" "19161"
"laterite-DOI IO" "302210" "302210"
"laterite-DOI 12" "258159" "214865"
"laterite-DOI 13" "214865" "231473"
"laterite-DOI 16" "231473" "203491"
"laterite-DOI Ig" "203491" "204186"
"podchrome-DDDOI" "228019" "228019"
"podchrome-DDD02" "208319" "208319"
"podchrome-DDDD3" "4653" "252321"
"podchrome-DDDCL4" "251670" "251670" ];
3 Comments
More Answers (2)
Image Analyst
on 8 Mar 2019
Are you using %s instead of %f? Try this - it works!
Results = [
"later ite-DD030" "210801" "210801"
"later ite-DD036" "28160" "28160"
"later ite-DDU7" "127198" "127198"
"later ite-DDC4g" "204186" "204186"
"laterite-DDOSO" "19161" "19161"
"later ite-DDD63" "302210" "302210"
"later ite-DDD68" "258159" "214865"
"later ite-DDDEg" "214865" "231473"
"laterite-DD073" "231473" "203491"
"later ite-DD074" "210801" "210801"
"laterite-DDD81" "28160" "28160"
"later ite-DDDB8" "127198" "127198"
"laterite-DOID8" "204186" "204186"
"laterite-DOIDg" "19161" "19161"
"laterite-DOI IO" "302210" "302210"
"laterite-DOI 12" "258159" "214865"
"laterite-DOI 13" "214865" "231473"
"laterite-DOI 16" "231473" "203491"
"laterite-DOI Ig" "203491" "204186"
"podchrome-DDDOI" "228019" "228019"
"podchrome-DDD02" "208319" "208319"
"podchrome-DDDD3" "4653" "252321"
"podchrome-DDDCL4" "251670" "251670" ];
numRows = size(Results, 1);
filename = fullfile(pwd, 'Results.txt');
fileID = fopen(filename, 'wt');
for row = 1 : numRows
fprintf(fileID, '%s, %s, %s\n', Results(row, 1), Results(row, 2), Results(row, 3));
end
fclose(fileID);
winopen(filename); % Pop open in the default text editor program.
0 Comments
See Also
Categories
Find more on Tables 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!