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
  • 111,146
  • 38
  • 238
  • 371
TEX
  • 2,249
  • 20
  • 43

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
  • 111,146
  • 38
  • 238
  • 371
1

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

I used strcat and num2str to create a filename.

Ander Biguri
  • 35,140
  • 11
  • 74
  • 120