Ben25
Ben25

Reputation: 131

Saving data from workspace to a specific path

I work with a dataset and want to save a variable from workspace to a directory in the form of, for example Label_1 , Label_2 , ... but unfortunately face with error.

for indImg = 1:100
.
.
.
  Label= ...     % this is a matrix
  savepath = './Data/50';
  save([savepath 'Label' '_' indImg],'Label');
end

Any ideas?

Upvotes: 0

Views: 43

Answers (1)

Paolo
Paolo

Reputation: 26073

The error is shown because indImg is a double, and it expects a char. You can convert it to a char with num2str.

You can use:

savepath = './Data/50';

for indImg = 1:100
.
.
.
  save(fullfile(savepath,['Label' '_' num2str(indImg)]),'Label');
end

Note that I moved the variable savepath outside the loop. You should do the same for any other variables which effectively do not change between loop iterations.

Upvotes: 4

Related Questions