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?

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I know it works but personally I don't usually see , for horizcat. +1 anyways
1

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

I used strcat and num2str to create a filename.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.