How to Update Label Text from Custom Function in AppDesigner?

Hello everyone! I have custom analysing function that analyses the directory. I am calling this function from the main GUI app in AppDesigner. The function that I am calling from the main app has the following structure,
function analyse_result = analyse(path_dir)
objects = objects_in_dir(path_dir)
for ii = 1:length(objects)
do_something
end
end
Now I've added a label text on the gui and called ProgressLabel. What I want to do is to update this label inside the function body, so I've updated the function like so,
function analyse_result = analyse(path_dir,app.ProgressLabel.Text)
objects = objects_in_dir(path_dir)
for ii = 1:length(objects)
do_something
app.ProgressLabel.Text = sprintf("Iteration: %%u",(ii/length(objects))*100)
end
end
But when I'm calling this updated function from the main gui with added new variable, nothing changes on the text label. How can I solve this issuse?

 Accepted Answer

Hello Arda,
Try using drawnow to update figures and callbacks as the following:
function analyse_result = analyse(path_dir,app.ProgressLabel.Text)
objects = objects_in_dir(path_dir)
for ii = 1:length(objects)
do_something
app.ProgressLabel.Text = sprintf("Iteration: %%u",(ii/length(objects))*100)
drawnow
end
end
Same is necessary for other operations such as while loop terminations via a button.

6 Comments

Hey, Ergin thanks for the quick reply but I already tried this beforehand, still no results.
Hello again,
Lets try two more things.
Do you use double % in the sprintf delibaretely? Single % and u should be enough for base 10, unsigned integers.
Other possibility is that the objects variable might be an empty array which can cause program to skip the loop, therefore left the label intact.
Could you please check these two possible causes?
Hello Ergin, thanks for the quick reply. I used % delibaretely to include % sign. Objects variable is not empty and for loop works through all of the iterations. But I think there is a problem where I pass the input variables for the function. I inserted breakpoints in the mainapp to see the workspace variables and saw that the app.ProgressLabel.Text variable does not pass through. How can I solve this? app.ProgressLabel.Text is global, I changed the function input as,
function analyse_result = analyse(path_dir,progressObject)
objects = objects_in_dir(path_dir)
for ii = 1:length(objects)
do_something
progressObject = sprintf("Iteration: %%u",(ii/length(objects))*100)
drawnow
end
end
and tried again. In the main gui I passed variables to the function as
analyse(path_dir,app.ProgressLabel.Text)
But this didn't solve the issue. progressObject shows empty in the workspace. I cannot pass through a global GUI variable to function variable.
Hey,
I think you don't need to pass progressObject into your function as an input, because you already create it in the loop.
I now noticed that you don't use app in your function input arguments. Try adding app into your inputs and after creating progressObject, try setting the label again. Kindly check the following code:
function analyse_result = analyse(app, path_dir,progressObject)
objects = objects_in_dir(path_dir)
for ii = 1:length(objects)
do_something
progressObject = sprintf("Iteration: %%u",(ii/length(objects))*100);
app.ProgressLabel.Text = progressObject;
drawnow
end
end
This might resolve your issue.
Silly of me, I couldn't see the app structure. Thanks a lot, it worked!
I'm happy that it worked, you are welcome.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2022a

Community Treasure Hunt

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

Start Hunting!