Recording values from a loop

4 views (last 30 days)
Alex Brown
Alex Brown on 11 Feb 2019
Edited: Kevin Phung on 11 Feb 2019
I am using a nested for loop to create an index of calculated values. I need it so everytime a value of T is calculated to be reported in order for the first iteration of the loop to the end. In this format:
where beds is the number of loops, n=beds
T1 T2 T3 ... Tn
1 2 3 ... n
Maybe this is really simple but I cannot get it to work, here's what I've tried
(Matlab R2016b)
while
%Calculations
for z=1:beds;
temps(z)=(T);
end
%Misc code
beds=beds+1;
end
returns 126 of the same T value as Tn
this code just run each temps off as the same value in a 1x126 double and this is not intended.

Answers (1)

Kevin Phung
Kevin Phung on 11 Feb 2019
Edited: Kevin Phung on 11 Feb 2019
You're missing indexing for your T
while
%Calculations
for z=1:beds;
temps(z)=T(z);
end
%Misc code
beds=beds+1;
end
however.. it looks like youre just setting temps to be equal to T-- do you even need the forloop?
  2 Comments
Alex Brown
Alex Brown on 11 Feb 2019
Edited: Alex Brown on 11 Feb 2019
Solution I found:
beds = 1; %% ORIGINALLY 0 SO CODE DIDN'T WORK!!!
while %Condition
z=beds;
%Calculations
temps(z)=[T]
beds=beds+1;
end
beds = beds - 1;
NB : I don't think z=beds is required, temps(beds) should work.
Kevin Phung
Kevin Phung on 11 Feb 2019
Edited: Kevin Phung on 11 Feb 2019
'NB : I don't think z=beds is required, temps(beds) should work.'
yep, it will save you some space and time, however insignificant. Glad you caught your error.
for future reference though, do try to provide as much information as possible so it'll be easier to answer your question!

Sign in to comment.

Categories

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

Community Treasure Hunt

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

Start Hunting!