MATLAB Answers

How can I store the value of variable 'dist_5thw​heel_to_se​cond_axle_​sem2_itr' in array/vector format with every for loop iteration such that previous value stays as well? ?

조회 수: 1(최근 30일)
MAYANK DHYANI
MAYANK DHYANI 2021년 8월 14일
댓글: Scott MacKenzie 2021년 8월 14일
dist_5thwheel_to_second_axle_sem2_itr = [7.13, 7.63];
dist_front_trailer_to_5thwheel_sem2_itr = [1.368, 1.668]; %variable whose value needs to be stored with each iteration
counter1 = length(dist_5thwheel_to_second_axle_sem2_itr);
counter2 = length(dist_front_trailer_to_5thwheel_sem2_itr);
a1 = zeros(1,4); %total number of simulation runs are 4, so preallocating with size 4 row-vector
for i1 = 1:counter1 %counter for parameter1
dist_5thwheel_to_second_axle_sem2 = dist_5thwheel_to_second_axle_sem2_itr(i1);
for i2 = 1:counter1 %counter for parameter2
dist_front_trailer_to_5thwheel_sem2 = dist_front_trailer_to_5thwheel_sem2_itr(i2);
a1 = dist_5thwheel_to_second_axle_sem2_itr(i1) ; %a1 should store all the values previous and updated
%assigniing the simulation parameters to base workspace
assignin('base','tmax',tmax);
assignin('base','dt',dt);
assignin('base','Vx',Vx);
assignin('base','X0',X0);
assignin('base','rdfname',rdfname);
assignin('base','rdfname_inner',rdfname_inner);
assignin('base','steer_input',steer_input);
assignin('base','brake_input',brake_input);
assignin('base','throttle_input',throttle_input);
assignin('base','Vx_input',Vx_input);
assignin('base','end_posy',end_posy)
assignin('base','steer_sens_c',steer_sens_c);
assignin('base','steer_ratio',steer_ratio);
assignin('base','steer_control',steer_control);
assignin('base','path_input',path_input);
assignin('base','lookahead_time',lookahead_time);
assignin('base','T_lag',T_lag);
modelname = 'SEC_final';
sim(modelname,[],simset('DstWorkspace','base'));
evalin('base',['save ..\simresults\',modelname,' s VR dt'])
end
end

채택된 답변

Scott MacKenzie
Scott MacKenzie 2021년 8월 14일
편집: Scott MacKenzie 2021년 8월 14일
You've got a bug in the setup of your loops. For the inner loop, change
for i2 = 1:counter1
to
for i2 = 1:counter2
As for maintaining the values of a1 from iteration to iteration, here's a simple way to do this that avoids pre-allocation. In the line before the outer loop begins, change
a1 = zeros(1,4);
to
a1 = []; % create a1, empty at first
Then, change the assignment to a1 in the inner loop to
a1 = [a1 dist_5thwheel_to_second_axle_sem2_itr(i1) ;
Do this, and you'll achieve the objective stated in your question.
  댓글 수: 2

댓글을 달려면 로그인하십시오.

추가 답변(0개)

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by