2

I want to load and save inside a nested loop in Matlab using "increasing" names, e.g.

for j=1:J
   for m=1:M
      load Bmj.mat
      ... A=...
      save A as Amj.mat
   end
end

Any suggestion?

Shai
  • 102,241
  • 35
  • 217
  • 344
TEX
  • 2,217
  • 19
  • 39

2 Answers2

2

You can use sprintf to format strings

for ii=1:J
    for m=1:M
        suffix = sprintf( '%d%d.mat', ii, m );
        load( ['B', suffix] );
        % process...
        save( ['A', suffix], 'A' );
    end
end

PS,
It is best not to use i as a variable name in Matlab.

Community
  • 1
  • 1
Shai
  • 102,241
  • 35
  • 217
  • 344
1

What about save(strcat('A',num2str(m),num2str(j),'.mat'),A)?

I used strcat and num2str to create a filename.

Ander Biguri
  • 33,979
  • 10
  • 76
  • 115