Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab file saving.

  1. Apr 19, 2008 #1
    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.

    for i=1:4
    save file-i a

    Of course this is not working :-)

    Do i need to use a string?
  2. jcsd
  3. Apr 19, 2008 #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


    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.
  4. Apr 19, 2008 #3
    By the way to output matrices to file...

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

    This saves the matrix M as binary data to the file 'i'
  5. Apr 20, 2008 #4
    Ah thx that was what I was looking for. Now I just need to fix how to save in the right format.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook