while loop in while loop
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
Walter Roberson
2017-1-9
编辑:Walter Roberson
2017-1-9
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.
采纳的回答
Niels
2017-1-9
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 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!