Writing to a text file

3 views (last 30 days)
Sina Shojaei
Sina Shojaei on 2 Mar 2015
Answered: Star Strider on 2 Mar 2015
Hi All, I want to save a matrix in a text file. fprintf and fwrite both create the .txt file in ASCII format which is not what I want. This is what I've got:
Cyle=[1 2 3];
fid=fopen('Output.txt','w');
fprintf(fid,char(Cyle),'char'); %// or fwrite(fid,char(Cyle),'char');
fclose(fid);
Is there a way around it?
Also, can I add a comment on the first line of the text file?
Thanks Sina

Answers (1)

Star Strider
Star Strider on 2 Mar 2015
If you don’t want to save it as an ASCII file, use the save function to save it as a binary file. You can also save the comment.
Example:
Cyle=[1 2 3];
Comment = 'I really like the way MATLAB solved this problem!';
save('MyData.mat', 'Comment', 'Cyle')
or alternatively:
save MyData.mat Comment Cyle

Categories

Find more on Environment and Settings in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!