MATLAB Answers

How to add a waitbar into an app created in App designer? Not in a separate figure.

262 views (last 30 days)
Austin Adam
Austin Adam on 17 Jan 2020
Commented: Adam Danz on 1 Apr 2020 at 14:55
I have done a lot of research on this and cannot seem to find a solution that is what I am looking for... maybe because what I am trying to do just isn't possible in Matlab, but I am sure there is a way.
I have an app that I am designing in App designer that is in the barebones stages right now... I want to implement a wait bar of some sort into the app along with the other components, i.e. not in a separate figure. Alot of what I have found has been using the waitbar function to open another figure with the waitbar, and I currently am using that right now, but want to get the waitbar inside my app.
I may not be able to use the matlab built in waitbar, and have already tried looking through the code but was not able to replicate the creation of it inside the app.
I know there are many options on the file exchange, but again, nothing that seems to be what I am looking for.
Does anyone know of a way, or can guide me in the right direction as to how I can approach this? Just imagine opening app designer, adding in a button or slider or whatever, and then adding in a waitbar to that same figure, that can update by pressing the button for example.
Hopefully someone has come up with a good solution for this by now and can help me out! Open to all suggestions.
Thank you very much,
Austin

  0 Comments

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 18 Jan 2020
Edited: Adam Danz on 21 Jan 2020
With AppDesigner you should be using uiprogressdlg() which displays a progress bar on top of your GUI within an external figure.
If you want progress bar functionality embedded within the app, you'll have to build it yourself. One way to do that is to create a long, rectangular axes that ranges from x=0:1. From within your code, you can set the size of a patch object (patch properties) that continually grows as you update its size and then vanishes after reaching 100%.
Here's a functional demo that creates an embedded progress bar and updates the progress within a loop. It also shows the percent complete.
% Set up the progress bar axis
fh = clf();
ax = axes(fh,'Position',[.1 .4 .8 .05],'box','on','xtick',[],'ytick',[],...
'color',[0.9375 0.9375 0.9375],'xlim',[0,1],'ylim',[0,1]); %gray94
title(ax,'Progress')
% Create empty patch that will be updated
ph = patch(ax,[0 0 0 0],[0 0 1 1],[0.67578 1 0.18359]); %greenyellow
% Create the percent-complete text that will be updated
th = text(ax,1,1,'0%','VerticalAlignment','bottom','HorizontalAlignment','right');
% Create processing loop that updates the progress bar
n = 100;
for i = 1:n
% update patch size and percentage text
ph.XData = [0 i/n i/n 0];
th.String = sprintf('%.0f%%',round(i/n*100));
drawnow %update graphics
end

  3 Comments

Austin Adam
Austin Adam on 24 Jan 2020
Hi Adam,
Thank you very much for the response! This was a perfect solution and is very easy to implement. I appreciate your help!

Sign in to comment.

More Answers (1)

balandong
balandong on 1 Apr 2020 at 12:14
To add on @adam answer, the following update are to be made if one desire this to be used with app designer.
% Set up the progress bar axis
fig=app4;
fig.Panel.AutoResizeChildren = 'off'; % To avoid using the whole uifigure space, advisable to place under the panel
ax = subplot(1,1,1,'Parent',fig.Panel);
ax.Position=[.1 .4 .8 .05]
ax.Box='on'
ax.XTick=[];
ax.YTick=[];
ax.Color=[0.9375 0.9375 0.9375];
ax.XAxis.Limits=[0,1];
ax.YAxis.Limits=[0,1];
title(ax,'Progress')
% Create empty patch that will be updated
ph = patch(ax,[0 0 0 0],[0 0 1 1],[0.67578 1 0.18359]); %greenyellow
% Create the percent-complete text that will be updated
th = text(ax,1,1,'0%','VerticalAlignment','bottom','HorizontalAlignment','right');
% Create processing loop that updates the progress bar
n = 100;
for i = 1:n
% update patch size and percentage text
ph.XData = [0 i/n i/n 0];
th.String = sprintf('%.0f%%',round(i/n*100));
drawnow %update graphics
end

  1 Comment

Adam Danz
Adam Danz on 1 Apr 2020 at 14:55
Tip: you can store the handle to the patch object and the text as an app property so those objects can be accessed anywhere within the app. See these instructions to store variables as app properties.

Sign in to comment.

Sign in to answer this question.