Output arguments in App Designer
84 views (last 30 days)
Show older comments
I would like to have functions that have some output parameters (and able to return these values to caller). Is it possible in App designer? I already checked the answers in community and got some work-around solution ideas.
I am currently using prerelease 2021a and this issue has been pending since 2016. Still don't have any `real` solution for it?
Thanks in advance.
Some edits regarding to Matt`s answer:
As the application whole, I want it to send some values back to me. So that, when the application window (aka. figure) is closed, those values are returned to me.
I'm waiting for this application figure to work like a dialog box.
Thanks Matt.
Answers (3)
Matt J
on 2 Jan 2021
[out1,out2]=app.func(in1,in2)
4 Comments
Walter Roberson
on 3 Jan 2021
In traditional figures, functions such as questdlg() work by creating graphics objects, setting up callbacks, and doing a waitfor() or uiwait(), extracting the data, deleting the graphics objects, and then returning the data.
One of the items I linked to suggested doing the same thing: running the app to create the graphics, doing a waitfor or uiwait, extracting the values, deleting the graphics objects, and then returning the data.
If that is not an acceptable solution for your situation, then you might have to wait a while for an acceptable solution to be implemented by Mathworks, as they do not provide any wrapper for that purpose in any released version.
Mario Malic
on 3 Jan 2021
Edited: Mario Malic
on 3 Jan 2021
If you want to do this "As the application whole, I want it to send some values back to me. So that, when the application window (aka. figure) is closed, those values are returned to me. ", then you could write a CloseRequestFcn that will assign variables in the workspace (should you do it? No).
I would suggest you to create a function(as mentioned by Walter) that would create the uifigure (if the app is simple enough) because function can easily return values to workspace, here's my example on the File Exchange.
By the way, App Designer does not support output arguments, as mentioned by some of the staff in here.
Lior de Marcas
on 16 Nov 2021
I am a novice with App Designer, so if someone have any problems with anything I have written here, fill free to comment and help me learn :)
I recently came into the same problem - I wanted to create a small UI, that ask user to perform some task (multi-image labelling, while playing a video in order to help them). I wanted to use App Designer, as building UI programmatically only is a pain. Ideally, users can:
- Write their own functions that can used the UI produced labels.
- Run the UI from command window, and continue using cm if they want (non-modal window).
I found 2 possible solutions:
More general one: have the uifigure "CloseRequestFcn" save to mat file any output you want. Then you can wait for UI to close (when calling from function, use "waitfor"), and load this file. Pros: Easy to do, keep all of App Designer abilities. Saving any hard collected user input in not necessarily a bad idea. Cons: This is somewhat "poofing" variables into existence, similar to the option @Mario Malic did not recommend above. Also great way to save unneeded files (especially if you will save this output later, after more analysis). May be slow.
Less general: save wanted outputs as properties, then make sure your app object "live" after the uifigure was closed. However I found out that something in matlab.apps.AppBase class make sure the app object is deleted when its associated uifigure is closed, and I haven’t found simple workaround for that.
So I exported my app to .m file (one of the options under "save"), and changed it to be a "handle" class -
classdef my_UI < handle
inside the app Constructor remove the "registerApp", switch the function "runStartupFcn" with a simple call to your "startupFcn".In the createComponents function, any component callback switch from:
createCallbackFcn(app, @myCallBack, true);
To:
@(~,evt) myCallBack(app,evt)
More changes might be needed in case your app use more App Designer abilities that I'm not familiar with.
Pros: now your app is an object with graphic fields instead of being dependent on the figure (Why this dependency is needed anyway?). No need to save any variable - it is all in the class, no variable was "poofed" into later code. Cons: for more complex app may take a little longer (not very time consuming in any case I think). You may lose some of App Designer due to this change (I am not familiar with App Designer enough to say).
I think all in all, the first option in better, even if the secound is more "nice looking".
0 Comments
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!