Saving Matrices Generated by Matlab Algorithm

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
3 replies · 12K views
PhilippH
Messages
12
Reaction score
0
Hi I have a question, I have to save to file the matrixs generated by an algorithm in Matlab. So i was wondering if is it possibile and how, to use in the filename a variable of the algorithm.

Let's say "i" is the counter of my cicle and i would like to save the matrix generated at every cicle to a file with the name NAMEFILE-i.

i.e.
for i=1:4
a=i;
save file-i a
end

Of course this is not working :-)

Do i need to use a string?
 
Physics news on Phys.org
you need to use the function int2str

mystr = int2str(i);

alternatively if you want to make the file name "NAMEFILE-i" then you can use

sprintf(mystr,'NAMEFILE-%i',i);

you'll have to check the syntax of this though, i can't remember it exactly off-hand. note that %i in the second argument refers to an integer type, not the variable i, whereas i in the last argument is the variable i.
 
By the way to output matrices to file...

outfile = fopen(int2str(i),'w');
dummy = fwrite(outfile,M,'float64');
fclose(outfile);

This saves the matrix M as binary data to the file 'i'
 
Ah thanks that was what I was looking for. Now I just need to fix how to save in the right format.