while loop in while loop
3 views (last 30 days)
Show older comments
Hi, I have a problem and i cant figure out why this code wouldnt work:
while 1
tic;
while(calllib('gestic', 'gestic_data_stream_update', gestic, 0)~=0)
plot3(gestic_pos.Value.x/65533,gestic_pos.Value.y/65533,gestic_pos.Value.z/65533,'o');
xlim([0 1]);
ylim([0 1]);
zlim([0 1]);
grid();
drawnow();
end
%check for abort
if ~isempty(k)
if strcmp(k,'s')
disp('STOP');
break;
elseif strcmp(k,'p');
disp('PAUSE');
pause;
k=[];
else
k=[];
end
end
te=toc;
while te<0.01
te=toc;
end
toc;
end
The overall while loop is executed until a certain key is pressed on the active figure. The first while reads position data from a stream and plots it. The second while should check if 10 milliseconds have passed until the loop continues.
The Problem is that MATLAB does not enter the first while loop. But when i delete the second while loop the code runs as desired.
The second while loop is needed to achieve a constant sample time.
I solved this problem by including the second while loop into the first but i dont understand why the original code doesnt work.
Can anybody explain it to me?
3 Comments
Walter Roberson
on 9 Jan 2017
Edited: Walter Roberson
on 9 Jan 2017
If you were using timer objects then you would normally not pause at all; instead you would configure the timer object for constant interval for the callbacks.
Accepted Answer
Niels
on 9 Jan 2017
As walter said try to replace
te=toc;
while te<0.01
te=toc;
end
toc;
With
te=toc
if te<0.01
pause(0.01-te)
end
toc
0 Comments
More Answers (0)
See Also
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!