How to save variables generated inside for loop into a structure

17 ビュー (過去 30 日間)
Tiago Goncalo
Tiago Goncalo 2021 年 5 月 24 日
回答済み: Mario Malic 2021 年 5 月 24 日
Hi everyone,
Is there a way to change this code to save all variables generated inside for loop in a more quick way? I have 30 variables, and manually putting them in a structure like line 6 and 7 is a bit exhausting.
Struct = cell(5,1)
c = 5;
for k=1:5
a=k
b=2*k
Struct{k}.a = a;
Struct{k}.b = b;
end
Thanks in advance

回答 (1 件)

Mario Malic
Mario Malic 2021 年 5 月 24 日
Hello,
field1 = 'f1'; value1 = zeros(1,10);
field2 = 'f2'; value2 = {'a', 'b'};
field3 = 'f3'; value3 = {pi, pi.^2};
field4 = 'f4'; value4 = {'fourth'};
s = struct(field1,value1,field2,value2,field3,value3,field4,value4)

カテゴリ

Find more on Structures in Help Center and File Exchange

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by