0

I have a for loop that goes through 7 elements in an array and does an operation, and gets a 'work' (name of variable) variable. I want to save this work variable in the form of 'work_#ofexperiment' (experiments 1 through 7)

So I have experiment 1, 2, 3, 4, 5, 6, 7

And an array [stuff1,stuff2,stuff3,stuff4,stuff5,stuff6,stuff7];

for i=1:7

do stuff work = something save work based on the order of i and name it work_experiment#

save('work_%s',work,experiment(i)) is not working

Alvin Nunez
  • 247
  • 1
  • 10
  • You're almost there... you need to use `sprintf` instead of directly adding the string to `save`. I'm sure there is a duplicate of this somewhere... looking... – beaker Aug 07 '15 at 18:48
  • Maybe this will help: http://stackoverflow.com/questions/11673674/matlab-iterative-filenames-for-saving – beaker Aug 07 '15 at 18:58

1 Answers1

0

You need to pass names of variables as strings to the save function.

save(sprintf('work_%d',i), 'work', 'experiment')

With save function you cannot do partial saving like experiment(i). Once saved you can partially update the mat file using matfile function. (http://www.mathworks.com/help/matlab/ref/matlab.io.matfile-class.html).

matObj = matfile('work_%s')
matObj.Properties.Writable = true
matObj.experiment(i) = newValue;
Navan
  • 4,407
  • 1
  • 24
  • 26