MATLAB: How to save variables to a specific M file dynamically without coverage of the next cycle in a circulation

cyclesave file

I wrote a M file which includes a circulation.I'd like to save the variable created in each cycle to the same M-file.However,it looks like that each cycle the new variable wrote in the file will cover the last one and only the variable in the final cycle could be saved if I put the code{save('filename.mat','variable'}in the circulation. How to solve it?

Best Answer

You need to do one of the following:
  • retain all of the previous output and write it all out; for example by using cell arrays. This requires as much memory as all of the outputs put together
  • save to a different file each time. Afterwards, merge all the data together if you need to
  • write the data out to a binary file that you put some kind of structure on. Afterwards, read the binary file and save it as the .mat that you need.
  • save to a different variable name in the same .mat file, using the -append option
  • use matFile to write to a different part of the same variable each iteration