How to have variables displayed in workspace after every while loop iteration?

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 个评论

"variables are only stored in the workspace after the entire loop is completed" &nbsp 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"?
as this loop is running nothing appears in the workspace (the side box by the matlab command window) until my loop( i=1:60 ) is completed
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&nbsp aa &nbspis used and the result is display in each iteration.
@per If you add
drawnow %or
pause (1)
then the data is shown in each iteration of the loop (in R2014b on windows anyway)
I tried, neither &nbsp pause(1) &nbsp nor &nbsp drawnow &nbsp helps in R2013a.
ok thank you so much I will try that with an updated matlab version. But just to clarify, although the variable is not stored in the workspace I can still use it while the loop is still running? for example I am having two codes run in parallel in matlab where one code(the one mentioned here) is receiving data from a sensor and the other code is pulling that data.
Also, not sure if I can ask additional questions but my matlab code is reading data from an arduino sensor that reads data when something is placed in front of the sensor(proximity sensor). so with the code i posted before, if nothing is in front of the sensor then my matlab code is terminated and I get the following error: In an assignment A(I) = B, the number of elements in B and I must be the same.
However when i do a while 1 loop :
s = serial('COM3');
set(s,'BaudRate',9600);
fopen(s);
out=[];
while 1
value = fscanf(s);
out = str2num(value);
disp(out);
end
fclose(s)
This does what I want, however it only saves the last number from the loop. I tried doing out(length(out) + 1) = str2num(value), but that still gives me an error when the sensor is not reading anything.
  • 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
Regarding "additional questions" &nbsp 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); &nbsp might work.

请先登录,再进行评论。

 采纳的回答

The easiest way (without seeing your code) is to initialise a counter before the loop, increment it within the loop, and subscript the variables calculated in the loop:
k1 = 1;
while some_condition
a(k1) = some_expression;
b(k1) = some_other_expression + a(k1);
k1 = k1+1;
end
Here, ‘a’ and ‘b’ will be available as vectors at the end of the loop, and available to each other within the loop. That’s as specific as I can get.

8 个评论

s = serial('COM3');
set(s,'BaudRate',9600);
fopen(s);
out=[];
for i=1:60
value = fscanf(s);
out(length(out) + 1) = str2num(value);
disp(out);
end
fclose(s);
above is my code, how would i be able to use out(k1) when im doing out(length(out)+1)? Thank you!
Since ‘value’ seems to be a scalar value, and you’re looping over ‘i’, change your loop to:
out = nan(1,60); % Preallocate
for k1=1:60
value = fscanf(s);
out(k1) = str2num(value);
disp(out(k1));
end
Thank you for your help, but my question is that out is not being displayed in the workspace as the loop is running because I want to be able to access the variable while the loop is still running in another code that will be running in parallel. Also if you dont mind can you please take a look at my other question above^. Thank you!
My pleasure!
I should have subscripted ‘out’ in your disp call. I corrected it in the code I previously posted.
You didn’t mention what line was throwing the error, but I suspect that the sensor is returning an empty value [] when it’s not reading anything. You need to check for that and write something else to the array if the returned value is empty. I would do something like this:
out = nan(1,60); % Preallocate
for k1=1:60
value = fscanf(s);
if ~isempty(value)
out(k1) = str2num(value);
else
out(k1) = NaN;
end
disp(out(k1))
end
I didn’t test that code, but it should work.
Yes!! That does work thank you so much! Is there any way though instead of displaying NaN , you can tell Matlab to do nothing? For "out(k1) = NaN", is there anyway instead of filling NaN in the matrix, I can just not enter anything and tell Matlab to do nothing?
My pleasure!
No, that was the problem in the first place, because MATLAB can’t put ‘nothing’ in a matrix. It has to put something there, and ‘empty’ literally doesn’t count. The NaN enteries are simply placeholder values to make MATLAB happy if your sensor doesn’t have anything to look at. An alternative could be a cell array that does allow ‘empty’ values. To do that, replace the parentheses in ‘out(k1)’ with curly brackets: ‘out{k1}’. (I would use a cell array.)

请先登录,再进行评论。

更多回答(1 个)

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)])

1 个评论

Thank you for your help, but my question is that out is not being displayed in the workspace as the loop is running because I want to be able to access the variable while the loop is still running in another code that will be running in parallel. Also if you dont mind can you please take a look at my other question above^. Thank you!

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by