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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by