Saving Matrices Generated by Matlab Algorithm

In summary, the conversation discusses the process of saving matrices generated by an algorithm in Matlab to a file using a variable in the filename. It is suggested to use the function int2str or sprintf to create the desired file name. Additionally, the process of outputting matrices to a file is also mentioned, using the functions fopen, fwrite, and fclose.
  • #1
PhilippH
12
0
Hi I have a question, I have to save to file the matrixs generated by an algoritm in Matlab. So i was wondering if is it possibile and how, to use in the filename a variable of the algoritm.

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
  • #2
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.
 
  • #3
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'
 
  • #4
Ah thanks that was what I was looking for. Now I just need to fix how to save in the right format.
 

What is a matrix in Matlab?

A matrix in Matlab is a two-dimensional array of numbers. It can be used to store data and perform mathematical operations.

Why is it important to save matrices generated by Matlab algorithms?

It is important to save matrices generated by Matlab algorithms because they may contain important data or results that can be used for future analysis or reference. Saving them also allows for easy retrieval and sharing with others.

How can I save matrices generated by Matlab algorithms?

To save matrices generated by Matlab algorithms, you can use the built-in "save" function. This allows you to specify the name of the file and the variables you want to save.

What file format should I use to save matrices generated by Matlab algorithms?

The recommended file format to save matrices generated by Matlab algorithms is .mat, which is a binary format specifically designed for storing Matlab variables and data.

Can I open and use saved matrices on different computers?

Yes, as long as the computer has Matlab installed, you can open and use saved matrices on different computers. However, ensure that the versions of Matlab are compatible to avoid any compatibility issues.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
18
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
784
  • Programming and Computer Science
Replies
1
Views
417
  • Programming and Computer Science
Replies
3
Views
256
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
910
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Back
Top