how to save a .mat file full of characters into a .txt file?
Afficher commentaires plus anciens
Hello everyone:
Thank you in advance for giving me all the suggestions.
I am trying to export a .mat file full of characters into a .txt file. Part of the .mat file looks like the following:
zentimeter zepler zero zeros zeroth zeta ziel zig zigxag zimmermann zinc zirconate zisler zncl zns zobel zobels zodiacal zolatarev zonal zone zones zurich
After loading it to the workspace, I tried save command but it did not work and later I tried the command: [row]=find (dictionary). Instead of displaying the characters, a lot of numbers were displayed. May I know how to solve this problem please?
Thank you very much.
Best regards,
Wen
Réponses (2)
Matt Fig
le 10 Mar 2011
You don't say what form dictionary takes. Is it a cell array, a character array, or what?
In general, use the FWRITE function to save the data:
fid = fopen('mydata.txt','wt')
fwrite(fid,dictionary) % This line depends on your data, see above.
Walter Roberson
le 10 Mar 2011
If the data to be written is all one long string separated by spaces, and you want it to be output in that form, then:
fid = fopen(OutputFileName, 'wt');
fwrite(fid, TheString, '*char');
fclose(fid);
If you do this, then the strings will be UTF encoded, probably to UTF-8. UTF-8 represents the basic ASCII characters as themselves but uses multiple bytes to write out non-ASCII characters.
If the data is stored in a cell array of strings and you want it written out one entry per line, then
fid = fopen(OutputFileName, 'wt');
for K = 1 : numels(TheCellArray)
fprintf(fid, '%s\n', TheCellArray{K});
end
fclose(fid);
This too will probably use UTF-8 encoding.
2 commentaires
Jan
le 10 Mar 2011
Instead of the loop over K you can write:
fprintf(fid, '%s\n', TheCellArray{:});
To write UTF8 encoded file, you need:
fid = fopen(File, 'w', 'n', 'UTF-8');
Walter Roberson
le 10 Mar 2011
Good point about the looping.
Using 'UTF-8' specifically forces UTF-8 coding; the default for fopen() is "system dependent". UTF-8 is the default on the server I use... and I did say "probably" ;-)
Catégories
En savoir plus sur Language Support dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!