MATLAB Answers

Greg
3

Call an MLAPP with input argument(s)

Asked by Greg
on 13 May 2016
Latest activity Commented on by Greg
on 16 Oct 2017
Typically, in a classdef, you define input arguments in the class constructor. Within appdesigner, the class constructor is written automatically, and is locked out to prevent problems.
Has anybody found a different way to pass input arguments to an appdesigner MLAPP?
EDIT: This has been resolved with R2017b. For releases prior to R2017b, the accepted answer by Chris Portal is still the best workaround.

  0 Comments

Sign in to comment.

6 Answers

Answer by Chris Portal on 21 May 2016
 Accepted Answer

A workaround for now is to create a public function in your app (use the "+ Function" in Code View and choose the "public" option) and use that to pass in any "input variables" after the app is created. For example:
hApp = myapp;
MySetupFunction(hApp, input1, input2);
It's not the proper way, but it's the closest way of working around the limitation for now.

  8 Comments

Kathy
on 11 Aug 2017
Hi Adam, I got the error No appropriate method, property, or field 'setSelection' for class 'matlab.ui.control.ListBox'.
But I finally figured it out also using the function SomeButtonCallback( mlappA, event)
hAppB = mlappB;
hAppB.someListbox.Value=hAppA.firstlistbox.Value
This initialized my listbox in my second file with the value from my first file.
Thanks so much for your help!!
Adam
on 11 Aug 2017
Yes, my 'setSelection' was just a place holder for whatever function you chose to add on your second app to set the selection. The expectation was you would create that as a function (with your chosen name) and then put relevant code inside it.
Kathy
on 11 Aug 2017
Thanks, that makes sense. I'm fairly new to Matlab and especially App Designer so thanks for your patience, I appreciate the help!

Sign in to comment.


Answer by Melissa Williams on 16 Oct 2017

In R2017b, App Designer supports input arguments for apps. From the Run button in the toolstrip, select the drop down and select "Add Input Arguments. Add the variable names you wish to use. App Designer will automatically create a startup function for you if one does not exist, and update the constructor correctly. Write the code needed to process the input arguments in your startup function.

  1 Comment

Greg
on 16 Oct 2017
I voted for you answer, but I'd like to leave the original "Accepted Answer" for those not fortunate enough to have R2017b. That way, they can still see the workaround.

Sign in to comment.


Answer by Adam
on 16 Dec 2016
Edited by Adam
on 19 Dec 2016

This is certainly an unpleasant limitation. This is my first foray into appdesigner after being very familiar with creating programmatic GUIs using classes. The uneditable constructor and component initialisation functions will certainly take me some getting used to. In GUIDE I am used to passing input arguments in and using them to initialise sliders etc within the OpeningFcn.
My solution to the problem of not being able to pass input arguments to the constructor will likely be to create a 'named constructor' as a public static function, e.g.
methods ( Static, Access = public )
function app = create( someStuff, someOtherStuff )
app = MyApp;
app.someStuff = someStuff;
initialiseSlider( app, someOtherStuff );
end
end
and then launch the app as e.g.
MyApp.create( 7, 15 );
It still means that the app is created before you can assign any of your properties or setup slider components based on inputs, but hopefully that will not represent a problem.
I hope appdesigner evolves to allow more standard class usage though. I understand why some of the code is locked to stop people screwing up the app by adding code that doesn't make sense, but for people who know what they are doing it is frustrating to have to use workarounds in code design.
Annoyingly the 'coding alerts' equivalent of M-Lint are very simplistic too. I get an alert saying I should use 'app' as the first argument to this create function even though it is in a Static methods block!

  0 Comments

Sign in to comment.


Answer by Terry Brennan on 25 Jan 2017

You can provide inputs through a subclass. If the app you created with AppDesigner is called myApp then create a subclass (not in AppDesigner, in an ordinary editor)
classdef myActualApp < myApp
methods % define the constructor
function mAA = myActualApp(inputs ...)
mAA = mAA@myApp; % this line is not really necessary
....
process inputs
...
end
end
end

  0 Comments

Sign in to comment.



As far as I know you can't in the official editor You can probably with an external editor...not sure what happens then though if open the file again in the official editor
But, may be you can insert a function to change the data members and call it using callback function.

  0 Comments

Sign in to comment.


Answer by Geoffrey Vincent on 10 May 2017

Hello, The app designer editor is locked. But you can copy-paste all the code from the mlapp in a classic .m file and run it like usual.
After this you can pass arguments through the constructor and the output method of the app class.
But if you still want to edit the GUI with the design view you cannot re-copy-paste the code inside the mlapp. So do the trick only when your GUI is close to be finish.

  0 Comments

Sign in to comment.