while loop in while loop
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Simon Hehenberger
am 9 Jan. 2017
Bearbeitet: Walter Roberson
am 9 Jan. 2017
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 Kommentare
Walter Roberson
am 9 Jan. 2017
Bearbeitet: Walter Roberson
am 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.
Akzeptierte Antwort
Niels
am 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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!