Saving Matrices Generated by Matlab Algorithm

Click For Summary

Discussion Overview

The discussion revolves around saving matrices generated by a Matlab algorithm to files, specifically how to incorporate a variable into the filename during a loop. Participants explore different methods for formatting the filename based on a loop counter.

Discussion Character

  • Technical explanation

Main Points Raised

  • One participant inquires about saving matrices to files with filenames that include a variable from a loop, specifically asking if using a string is necessary.
  • Another participant suggests using the function int2str to convert the loop counter to a string for filename creation.
  • A further suggestion involves using sprintf to format the filename as "NAMEFILE-i", noting the need to check the syntax for correctness.
  • Another participant provides an example of using fopen and fwrite to save a matrix as binary data, indicating that the filename can be derived from the loop counter.
  • A participant expresses gratitude for the information and indicates a need to resolve how to save the matrix in the correct format.

Areas of Agreement / Disagreement

Participants appear to agree on the methods for generating filenames based on a loop counter, but there is no consensus on the best approach or format for saving the matrices.

Contextual Notes

Some participants mention checking syntax and formatting, indicating potential limitations in their proposed solutions.

Who May Find This Useful

Individuals working with Matlab who need to save generated matrices to files, particularly those interested in dynamic filename generation based on loop variables.

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.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
8K
  • · Replies 18 ·
Replies
18
Views
6K
Replies
3
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 7 ·
Replies
7
Views
8K