How to put values from a for loop into an array table

I am running a for loop and want to put the values that matlab calculates into an array table. However, when I run the code, matlab correctly iterates through the for loop, but only shows the last value calcualted.
BTE = []
for CR_test_1 = linspace(10,20,20)
rpm = 1800; % engine speed at condition 1
W_dot_b_cmd = 15; % engine power at condition 1
irun = 1;
[GIE(irun),NIE(irun),BTE(irun),W_dot_brake(irun),IMEPg(irun),IMEPn(irun),BMEP(irun),PCP(irun),P_int(irun),Q_wall_frac(irun),theta,Tsave(irun,:), Psave(irun,:),Vsave(irun,:),HRRsave(irun,:),Q_dot_wall_save(irun,:)] = engine_calc(n_cyl,b,s,conrod,CR_test_1,rpm,theta_s,W_dot_b_cmd);
BTE
end

回答(1 个)

Try this:
BTE = []
rpm = 1800; % engine speed at condition 1
W_dot_b_cmd = 15; % engine power at condition 1
CR_test_1 = linspace(10,20,20);
for irun = 1:numel(CR_test_1)
[GIE(irun),NIE(irun),BTE(irun),W_dot_brake(irun),IMEPg(irun),IMEPn(irun),BMEP(irun),PCP(irun),P_int(irun),Q_wall_frac(irun),theta,Tsave(irun,:), Psave(irun,:),Vsave(irun,:),HRRsave(irun,:),Q_dot_wall_save(irun,:)] = engine_calc(n_cyl,b,s,conrod,CR_test_1(irun),rpm,theta_s,W_dot_b_cmd);
end

2 个评论

Thank you very much, that worked as I wanted it to. I have another question. If I wanted to run another iteration with a different rpm and W_dot_b_cmd, how i would add a new row to the already existing BTE array table? I want to compare the BTE values at the two different conditions of rpm and W_dot_b_cmd.
Try this (this first loop does the same thing as before, then the second loop does the same thing again but with the new rpm and W_dot_b_cmd values (still once for each CR_test_1) and the results are appended as new rows in the variables (BTE, etc.)):
BTE = []
rpm = 1800; % engine speed at condition 1
W_dot_b_cmd = 15; % engine power at condition 1
CR_test_1 = linspace(10,20,20);
N = numel(CR_test_1);
for irun = 1:N
[GIE(irun),NIE(irun),BTE(irun),W_dot_brake(irun),IMEPg(irun),IMEPn(irun),BMEP(irun),PCP(irun),P_int(irun),Q_wall_frac(irun),theta,Tsave(irun,:), Psave(irun,:),Vsave(irun,:),HRRsave(irun,:),Q_dot_wall_save(irun,:)] = engine_calc(n_cyl,b,s,conrod,CR_test_1(irun),rpm,theta_s,W_dot_b_cmd);
end
rpm = 2800; % whatever value you want
W_dot_b_cmd = 25; % whatever value you want
for irun = 1:N
[GIE(N+irun),NIE(N+irun),BTE(N+irun),W_dot_brake(N+irun),IMEPg(N+irun),IMEPn(N+irun),BMEP(N+irun),PCP(N+irun),P_int(N+irun),Q_wall_frac(N+irun),theta,Tsave(N+irun,:), Psave(N+irun,:),Vsave(N+irun,:),HRRsave(N+irun,:),Q_dot_wall_save(N+irun,:)] = engine_calc(n_cyl,b,s,conrod,CR_test_1(irun),rpm,theta_s,W_dot_b_cmd);
end

请先登录,再进行评论。

类别

在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

版本

R2020a

提问:

2021-12-13

评论:

2021-12-13

Community Treasure Hunt

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

Start Hunting!

Translated by