Saving multiple variables in a mat file from loop

4 Ansichten (letzte 30 Tage)
Kyriacos Patsalides
Kyriacos Patsalides am 1 Mai 2017
Bearbeitet: Mario Santana am 25 Jan. 2019
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 Kommentar
Stephen23
Stephen23 am 1 Mai 2017
Bearbeitet: Stephen23 am 1 Mai 2017
"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:

Melden Sie sich an, um zu kommentieren.

Antworten (1)

KSSV
KSSV am 2 Mai 2017
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 Kommentar
Mario Santana
Mario Santana am 24 Jan. 2019
Bearbeitet: Mario Santana am 25 Jan. 2019
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by