Saving multiple variables in a mat file from loop

조회 수: 4 (최근 30일)
Kyriacos Patsalides
Kyriacos Patsalides 2017년 5월 1일
편집: Mario Santana 2019년 1월 25일
I am trying to save multiple variables in a mat file from a loop. My loops has 10 iterations and every time I calculate 4 different variables. In every step all variables has the same size, ex. SE = [1,100], DE = [1,100], KE = [1,100] and CE = [1,100]. How can I save them all in every iteration?
  댓글 수: 1
Stephen23
Stephen23 2017년 5월 1일
편집: Stephen23 2017년 5월 1일
"How can I save them all in every iteration?"
Do you have a good reason for wanting to do that? It would be much more efficient to put all of those values into arrays, and then save the arrays after the loop. Simply use indexing to put the values into some preallocated arrays (indexing is very efficient) and then after the loop save the arrays using one save call (faster and more efficient than multiple calls).
Also read this:

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

답변 (1개)

KSSV
KSSV 2017년 5월 2일
n = 10 ; % loop count
filename='test.mat';
File = matfile(filename, 'Writable', true);
for i = 1:n
SE = rand(1,2) ;
DE = rand(1,3) ;
% save variables to file
File.SE(i,1:2) = SE ;
File.DE(i,1:3) = DE ;
end
clear File;
  댓글 수: 1
Mario Santana
Mario Santana 2019년 1월 24일
편집: Mario Santana 2019년 1월 25일
Good example, but what about SE = rand(2,2)?, I mean for matrix 2 dimensions plus loop dimension. Thanks.
-----------------------------------------------------------------------------------------------------------------------
Ok solved, using cells you can fit it into the requirements of matfiles.

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by