MATLAB: While loop in while loop

controlwhilewhile-in-while

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?

Best Answer

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