MATLAB Answers


How to write vectors line by line in a txt file using fprintf delimited by a space?

Asked by bh dhouha on 6 Aug 2015
Latest activity Edited by Stephen Cobeldick on 6 Aug 2015
I have for example these three vectors : [1 2 3 ], [4 5 6 ],[7 8 9]. I would like to get the result like this in the file txt:
1 2 3 4 5 6 7 8 9
(each vector in a line) I tryed this
fid= fopen('h.txt','w');
but i didn't get the result. Delimiter ' ' doesn't work with this command.


Sign in to comment.


1 Answer

Answer by Adam
on 6 Aug 2015
 Accepted Answer

fprintf(fid,'%.5f ',h0);
should be enough. Note the space in the string.


So i don't have to open and close the file everytime i add a vector to the file ? because in my program i have a for loop, in every iteration i add a vector to the file.
You can open the file once before the loop, and close it after the loop.

Sign in to comment.