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 view (last 30 days)
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

Accepted Answer

Scott MacKenzie
Scott MacKenzie on 14 Aug 2021
Edited: Scott MacKenzie on 14 Aug 2021
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 Comments
MAYANK DHYANI
MAYANK DHYANI on 14 Aug 2021
Edited: MAYANK DHYANI on 14 Aug 2021
Dear @Scott MacKenzie Thankyou for your valuable input. The counter1 was a typo in inner loop. The main point was that I had not been assigning the array a1 properly i.e., a1 = [a1 dist_5thwheel_to_second_axle_sem2_itr(i1) ;
The issue is resolved now.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!