How to save two matrices A and B in single .mat file using a loop (A and B changes for each operations)?
조회 수: 51 (최근 30일)
이전 댓글 표시
I have two matrices A and B. every time these matrics changes with some operations on image. So I would like to save them in a single .mat file using a loop, so that each .mat file will be saved in different name. now I want to load 1) .mat file using a loop 2) from .mat file I need to load A and B matrices separately for next operations.
how to do that? in other words how to save workspace in a loop and load them back using a loop?
댓글 수: 0
답변 (1개)
the cyclist
2017년 11월 24일
편집: the cyclist
2017년 11월 24일
Here is one way:
for i = 1:3
A = rand(4);
B = rand(5);
filename = ['file',sprintf('%d',i),'.mat'];
save(filename,'A','B')
end
This will save each of the newly created A and B into files named "file1.mat", etc.
Unless A and B are prohibitively large, perhaps a better way to do this would be to save your matrices into cell arrays, and save the cell array:
for i = 1:3
A{i} = rand(4);
B{i} = rand(5);
end
save('file.mat','A','B')
댓글 수: 1
Rik
2021년 3월 10일
Comment posted as flag by qusay hamad:
thank you it's a good way to save array into the mat file
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!