Saving output from function in different categories

1 回表示 (過去 30 日間)
Joel Schelander
Joel Schelander 2021 年 4 月 16 日
回答済み: Abhishek Gupta 2021 年 4 月 19 日
I have vehicles BEV, the variable can have the value BEVR, BEVU or BEVA.
I have households Household, the variable can have the value House, Apartment and [Apartment House]
The inner loop contains functions for how many households are considered.
My question is how I best can save the variables GUD and GUDID?
for i=1:0
if i==1
BEV=BEVA;
Household=Apartment
end
if i==2
BEV=BEVR;
Household=House;
end
%More if statements...
if i==9
BEV=BEVU
Househol=[Apartment House];
end
for z=1:3
if z==1
[GUD,GUDID]=H1(ID,HHPerson,nBEV,BEV,x,Hcombos,Household,sample);
end
if z==2
[GUD,GUDID]=H2(ID,HHPerson,nBEV,BEV,x,Hcombos,Household,sample);%run('H2')
end
if z==3
[GUD,GUDID]=H3(ID,HHPerson,nBEV,BEV,x,Hcombos,Household,sample);
end
end
end
I have so far tried this, but I need a way to save everything. I guess I can use sprintf within the for loop multiple times, but is there another thats faster?
save(sprintf('BEVU/Apu/AUG%d',z), 'GUD');
save(sprintf('BEVU/Apu/AUI%d',z), 'I2');

採用された回答

Abhishek Gupta
Abhishek Gupta 2021 年 4 月 19 日
Hi,
As per my understanding, you want to efficiently save "GUD" and "GUDID" variables in every loop iteration. One way would be to append "GUD" and "GUDID" variables at every iteration. Then, after the for-loop, you can save only these two variables, which contain the value of "GUD" and "GUDID" for every value of z. The sample code is shown below: -
% initialize output variables (let's say A and B)
A = []; B = [];
for z=1:3
%--- get GUD and GUDID ---%
% append the variables into the output variables
A = [A; {GUD}]; B = [B; {GUDID}];
end
% save the output variables
save('A.mat','A'); save('B.mat','B');

その他の回答 (0 件)

カテゴリ

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