Code simple custom waitbar

6 次查看(过去 30 天)
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 )
%CIRCLEWAIT Summary of this function goes here
% Detailed explanation goes here
if nargin==1
if x==1
pause(0.5)
close(gcf)
end
%This is where I have to update an existing circlewait?
f = findobj(allchild(0),'flat','Tag','CircleWait');
x=ceil(x*200); %current progress, no zero
newxdata=xdata(1:x); %only take a segment of the circle
newydata=ydata(1:x);
%This is where I have to pass newxdata and newydata to circle
%Undefined function or variable "xdata".
set(circle, 'XData', xdata, 'YData', ydata)
set(progress, 'String', [num2str(round(x*100)), ' %'])
else
%This is where I have to create a new circlewait
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; %12 'o clock at x=0 y=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

采纳的回答

Walter Roberson
Walter Roberson 2015-5-17
编辑:Walter Roberson 2015-5-17
drawnow() after you update the uicontrol String property.
Once you have done
f = findobj(allchild(0),'flat','Tag','CircleWait');
then
xydata = get(f, 'UserData');
xdata = xydata.xdata;
ydata = xydata.ydata;
x=ceil(x*200); %current progress, no zero
newxdata=xdata(1:x); %only take a segment of the circle
newydata=ydata(1:x);
%This is where I have to pass newxdata and newydata to circle
set(circle, 'XData', newxdata, 'YData', newydata)
In your code where you build the figure the first time, after you have done
for i=1:199
xdata(i+1)=-sin(s*i);
ydata(i+1)=cos(s*i);
end
Do
xydata = struct('xdata', xdata, 'ydata', ydata);
set(cwait, 'UserData', xydata);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by