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 (최근 30일)
  댓글 수: 4
AKASH TYAGI
AKASH TYAGI 2022년 6월 8일
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 2022년 6월 8일
"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.

댓글을 달려면 로그인하십시오.

채택된 답변

Dyuman Joshi
Dyuman Joshi 2022년 6월 8일
편집: Dyuman Joshi 2022년 6월 8일
%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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by