How do I print my data to a .txt file?

3 views (last 30 days)
Haven't been able to make sense of the info on MathWorks. All I'm trying to do is write the outputted vector from my function to a text file.
Here's what I've tried
fileID=fopen('solution.txt');
fprintf(fileID,'%e',x);
x is the outputted 1x5 or 1x10 vector. Just want to put it into a text. Any help on what I'm doing wrong?

Accepted Answer

Guillaume
Guillaume on 24 Mar 2015
Edited: Guillaume on 24 Mar 2015
Note that if all you want to do is write a single array to a text file and close the file, then dlmwrite is much simpler:
dlmwrite('solution.txt', x); %that's all, although you can go fancier if you want

More Answers (1)

Stephen23
Stephen23 on 24 Mar 2015
Edited: Stephen23 on 24 Mar 2015
according to the fopen documentation you need to supply the second option permission to tell it that you want to write to the file:
fid = fopen(filename,'wt');
You should probably use the text option too, as I have shown in my example above.

Community Treasure Hunt

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

Start Hunting!