Trying to control a simple "character bar spinner" with Start and Stop buttons

1 次查看(过去 30 天)
I would like to control the starting and stopping of the spinning action of the characters '| / - \' with Start and Stop buttons. In the below code, the Start button works just fine to start the spinning; but the Stop button (which I hoped would break the While loop, and thereby stop the spinning action) gives an error. Not sure why this is not working. Any illumination would be greatly appreciated. -Leith
handles.fig=figure;
handles.pb1=uicontrol('style','pushbutton','position',[100 200 100 40],'callback',@start_spin,'string','START');
handles.pb2=uicontrol('style','pushbutton','position',[250 200 100 40],'callback',@stop_spin,'string','STOP');
function start_spin(~,~)
idx=1;
j=1;
spin(idx,j);
end
function stop_spin(~,~)
j=0;
spin(idx,j);
end
function spin(idx, j)
ix = 1;
chars='|/-\';
while(idx==1)
if j==0
break;
end
fprintf(1,'\b%c', chars(ix));
ix = mod(ix,4)+1;
pause(0.05)
end
end

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by