Hello,
I am trying to code a very simple waitbar for my GUI. I have written it as a script that works just fine but I would like to convert it into a function that I can call just like Matlab's built-in waitbar:
h=mywaitbar(0, 'Please wait')
for i=1:100
mywaitbar(i/100)
end
close(h)
I took a look at Matlab's waitbar.m but I cannot figure out how to adopt the code for my waitbar. It is just a circle that starts at 12 'o clock and moves counterclockwise. This is the script I am currently using:
cwait=figure('Units', 'Pixels', 'Position', [500 500 300 200],...
'Color', [1 1 1], 'MenuBar', 'none');
screensize = get( groot, 'Screensize' );
figuresize = get( cwait, 'Position');
set(cwait, 'Position', [round((screensize(3)-figuresize(3))/2),...
round((screensize(4)-figuresize(4))/2), figuresize(3:4)]);
axes('Units', 'Pixels', 'Position', [85 60 130 130])
axis equal
axis off
hold on
xlim([-1.2, 1.2])
ylim([-1.2, 1.2])
progress=uicontrol('Style', 'text', 'Units', 'Pixels', 'Position', [120 25 60 20],...
'FontName', 'Calibri', 'FontSize', 16, 'String', '',...
'BackgroundColor', 'w');
k=50;
xdata=0;
ydata=1;
circle=plot(xdata, ydata, 'r', 'LineWidth', 10);
pos=2*pi/k;
for i=1:k
pause(0.03)
xdata(i+1)=-sin(pos*i);
ydata(i+1)=cos(pos*i);
set(circle, 'XData', xdata, 'YData', ydata)
set(progress, 'String', [num2str(round(i/k*100)), ' %'])
end
pause(0.5)
delete(cwait)
This is what I got so far but of course it is does not work yet:
function circlewait( x, str )
if nargin==1
if x==1
pause(0.5)
close(gcf)
end
f = findobj(allchild(0),'flat','Tag','CircleWait');
x=ceil(x*200);
newxdata=xdata(1:x);
newydata=ydata(1:x);
set(circle, 'XData', xdata, 'YData', ydata)
set(progress, 'String', [num2str(round(x*100)), ' %'])
else
cwait=figure('Units', 'Pixels', 'Position', [500 500 300 200],...
'Color', [1 1 1], 'MenuBar', 'none', 'Tag', 'CircleWait', 'Name', str);
screensize = get( groot, 'Screensize' );
figuresize = get( cwait, 'Position');
set(cwait, 'Position', [round((screensize(3)-figuresize(3))/2),...
round((screensize(4)-figuresize(4))/2), figuresize(3:4)]);
axes('Units', 'Pixels', 'Position', [85 60 130 130])
axis equal
axis off
hold on
xlim([-1.2, 1.2])
ylim([-1.2, 1.2])
progress=uicontrol('Style', 'text', 'Units', 'Pixels', 'Position', [120 25 60 20],...
'FontName', 'Calibri', 'FontSize', 16, 'String', '',...
'BackgroundColor', 'w');
xdata=zeros(1, 200);
ydata=xdata;
ydata(1)=1;
s=2*pi/200;
for i=1:199
xdata(i+1)=-sin(s*i);
ydata(i+1)=cos(s*i);
end
circle=plot(0, 1, 'r', 'LineWidth', 10);
end
Thank you for your help