while loop in while loop

3 ビュー (過去 30 日間)
Simon Hehenberger
Simon Hehenberger 2017 年 1 月 9 日
編集済み: Walter Roberson 2017 年 1 月 9 日
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 件のコメント
Simon Hehenberger
Simon Hehenberger 2017 年 1 月 9 日
Thank you. This Works :) I have now rewritten my methods and its working more precise with timer objects.
I still wonder why this code with two while loops didnt work XD
Walter Roberson
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
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 件)

カテゴリ

Help Center および File ExchangeGraphics Performance についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by