different result for simulink in for loop
Afficher commentaires plus anciens
greetings!
i generated code that play simulink in for loop like belows
but it has different results in for = 2 when for 1:3 and for 2:2
so i set new variables but it remains same.
how can i solve this?
optionsetting = {
[1, 2, 0], % FTP phase1
[1, 3, 0], % FTP phase2
[1, 2, 0], % FTP Phase3
[1, 3, 0], % FTP phase4
[2, 0, 0], % Hwy
[3, 1, 2], % US06 Phase1
[3, 2, 3], % US06 Phase2
[3, 3, 4]} % US06 Phase3
result_total = [];
variables_to_keep = who;
%%
for iii = 1 : 2
current_vars = who;
vars_to_clear = setdiff(current_vars, [{'iii', 'variables_to_keep'}, variables_to_keep']);
if ~isempty(vars_to_clear)
clear(vars_to_clear{:});
end
sim_index = iii;
soc_init = SOC_setting{sim_index}(1);
soc_end = SOC_setting{sim_index}(2);
on_line = onoffline_setting{sim_index}(1);
off_line = onoffline_setting{sim_index}(2);
option1 = optionsetting{sim_index}(1);
option2 = optionsetting{sim_index}(2);
option3 = optionsetting{sim_index}(3);
Select_driving_mode
Main
Lck_chg = (iii == 1 || iii == 3);
if iii == 6
OOL_factor = 1.2
elseif iii == 8
OOL_factor = 1.4;
else
OOL_factor = 1;
end
if option1 ==3
Shift_Gain_L = [1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00]*1.2;
Shift_Gain_H = [1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00]*1.35;
TCU_input
else
Shift_Gain_L = [1 1 1 1 1 1 1 1].*1.2;
Shift_Gain_H = [1 1 1 1.00 1.00 1.00 1.00 1.00]*1.2;
TCU_Input
end
out = sim('Simulater');
Fuel_Auto_ver2;
if iii==1
result_total = result_names;
end
clear out
result_total = [result_total result_values];
end
3 commentaires
Paul
le 19 Nov 2024
Hi Youngjin,
I don't have an answer becasue I can't follow the logic. But I can say that the code will be much better, more flexible, easier to understand, and easier to process outputs if you switch to using the Simulink.SimulationInput paradigm.
Youngjin
le 19 Nov 2024
Paul
le 21 Nov 2024
I don't understand why Simulink.SimulationInput won't work, but o.k.
Regardless, I sincerely doubt that the approach that looks like it's trying to clear different variables from the base workspace each time through the loop is the way go.
If you really must run from the base workspace (as opposed to inside a function), then it seems like it would be better to have a function that is called from the base workspace and returns the parameters needed to run the simulation. And if you want to be super careful, clear those variables from the base workspace prior to next call to sim. That way you know exactly what parameters are used for each call to sim.
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!