How can I create and save a variable that contains filename ?

2 views (last 30 days)
I would like to create and save a variable that contains the file number given in a string, for example:
file='1';
a=[1 2 3];
name = ['a' num2str(file), '=a'];
eval(name);
which creates a1=[1 2 3]
How can I now save this variable a1 in a mat file (without need to write a1, because this should work in a for loop, where 1 in incremented) ? Thanks

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 8 Jun 2013
Edited: Azzi Abdelmalek on 8 Jun 2013
name='a1'
save('yourmatfile',name)
But this is not a good idea, if you explain what you want to achieve, maybe there are better ways.
  2 Comments
Paola
Paola on 8 Jun 2013
Consider the following example:
b=[1 2 3];
for ii=1:5
a=b*ii;
name = ['a' num2str(ii), '=a']
eval(name);
filename=['test' num2str(ii) '.mat'];
save(filename, 'xxx')
end
test1.mat, that should contain a1, then test2.mat should contain a2 and so on. what should xxx be to do that ?
Azzi Abdelmalek
Azzi Abdelmalek on 8 Jun 2013
Edited: Azzi Abdelmalek on 8 Jun 2013
b=[1 2 3];
for ii=1:5
a=b*ii;
name = [sprintf('a%d',ii) '=a']
eval(name);
filename=sprintf('test%d.mat',ii);
save(filename, sprintf('a%d',ii))
end

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 8 Jun 2013

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!