Dynamically naming and exporting MAT files

Question

I've tried to search stackoverflow for a solution, but the cases I found differ slightly from what I'm trying to do, so I thought I'd ask.

I have a loop in MATLAB, which for every iteration, a large matrix is computed. I want to save each matrix as a separate MAT file, however each file needs to be named according to its position in loop. For example: matrix1, matrix2,...

The method I'm using to save my data (which seemed different from the few examples I found) is the following (where matrix is the generated matrix and matrix1 is the filename to be saved for the matrix corresponding to i = 1)

 save matrix1 matrix;

I've seen something similar to

 save ['matrix', i] matrix;

But I can't seem to remember the exact syntax.

Sorry if the question is very basic, a simple nod in the right direction for this type of saving would be greatly appreciated.


Show source
| save   | matlab   2017-01-02 18:01 2 Answers

Answers ( 2 )

  1. 2017-01-02 18:01

    Here's my nod:

    eval(['save matrix' num2str(i) ' matrix;']);
    

    Good luck! :)

  2. 2017-01-02 19:01

    Use the functional form of save:

    save(['matrix', int2str(i)], 'matrix');
    
◀ Go back