How to save multiple matrices in multiple files? I solved the first two parts using for loop but couldn't do the 3rd one.How to save these multiple matrices in separate files?

2 views (last 30 days)
  4 Comments
AKASH TYAGI
AKASH TYAGI on 8 Jun 2022
Thank you for the reply @Dyuman Joshi .
Here is what I wrote for first two and what i tried for the 3rd one.
for n=20:5:50
A=magic(n)
outputfile=['magicmatrix',int2str((n/5)-3)]
end
This solved the first two parts.
Then I ran out of ideas! I tried-
save magicmatrix1 A
It saved the last one (Magic(50) ) in magicmatrix1 file.
Issue is I have 7 separate matrices saved in A.. and I want to save each of them separately in separate files starting from magicmatrix1 , magicmatrix2 ... so on. To be honest, I don't know what to do.
Thanks for reading and please help if you can.
Stephen23
Stephen23 on 8 Jun 2022
"So challenge remaining is store the magic matrices in different names say A1, A2 etc. "
You could do that if you want to force yourself into writing slow, complex, inefficient, obfuscated, buggy code that is difficult to debug.
Or you could:
  • just write each array to file, without storing them all, or
  • use indexing, which is neat, simple, and very efficient.

Sign in to comment.

Accepted Answer

Dyuman Joshi
Dyuman Joshi on 8 Jun 2022
Edited: Dyuman Joshi on 8 Jun 2022
%Saving magic matrices in files
for n=1:7
A=magic(5*n+15);
filenames=sprintf('magicmatrix_%d.mat',n);
save(filenames);
end
%clear workspace variables
clear all
%Loading saved files
for j=1:7
filenamel=sprintf('magicmatrix_%d.mat',j);
y=load(filenamel,'A');
figure %creates a new figure window
image(y.A) %display image
end
  3 Comments

Sign in to comment.

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!