How to have variables displayed in workspace after every while loop iteration?
Show older comments
Hi all,
I have a while loop in which my variables are only stored in the workspace after the entire loop is completed. Is there any way that I can store the variable in the workspace after each iteration of the loop, because I need to use these values while the loop is still running. Thank you!
8 Comments
per isakson
on 12 Nov 2014
Edited: per isakson
on 12 Nov 2014
"variables are only stored in the workspace after the entire loop is completed"   sounds really strange to me. Variables are created when assigned values for the first time. They are immediately visible in the workspace, in which they are created.
- What "workspace" do you refer to?
- What do you mean by "stored"?
- How do you detect that they are not "stored"?
Nancy
on 12 Nov 2014
per isakson
on 12 Nov 2014
Edited: per isakson
on 12 Nov 2014
I had to do a little experiment. I run the script below with [Run Selection].
clear aa
ii = nan( 1,6 );
for jj = 1 : 6
aa = 12 + jj;
ii(jj) = aa - 12;
disp( ii )
end
which display in the command window
1 NaN NaN NaN NaN NaN
1 2 NaN NaN NaN NaN
1 2 3 NaN NaN NaN
1 2 3 4 NaN NaN
1 2 3 4 5 NaN
1 2 3 4 5 6
You are correct, the workspace window is not updated until the loop is completed. However, the variables may be used. In this case  aa  is used and the result is display in each iteration.
Robert Cumming
on 12 Nov 2014
@per If you add
drawnow %or
pause (1)
then the data is shown in each iteration of the loop (in R2014b on windows anyway)
per isakson
on 12 Nov 2014
Edited: per isakson
on 12 Nov 2014
I tried, neither   pause(1)   nor   drawnow   helps in R2013a.
Nancy
on 12 Nov 2014
per isakson
on 12 Nov 2014
- YES, the variable can be used!
- Regarding the words, in this case I think displayed in the workspace window is better than stored in the workspace
per isakson
on 12 Nov 2014
Regarding "additional questions"   Your chance to get a good answer increases if you make it a separate question with an appropriate subject line.
out(end+1) = str2num(value);   might work.
Accepted Answer
More Answers (1)
Andrew Reibold
on 12 Nov 2014
Edited: Andrew Reibold
on 12 Nov 2014
Not sure if you are having issues with the worksplace, or if you are actually trying to say you are having trouble displaying values to the screen while running the program
Instead of using
out(length(out) + 1) = str2num(value)
Can you use...?
out(i) = str2num(value)
Then, can you use...?
disp(out(i))
Or if you want to keep track of which iteration you are on also,
disp(['Iteration ',i,'- Out value of ',out(i)])
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!