How to properly structure code in appdesigner?

21 views (last 30 days)
Hi.
Assume having a complex GUI with dozens of private functions. What is the best way to split the code into separate files? How can I tell MATLAB that there is a private function, but the implementation of that function is done in a separate file?
  2 Comments
Inception
Inception on 17 May 2022
Not necessarily private functions. I am also happy to do it with public functions.
Kevin Holly
Kevin Holly on 17 May 2022
As for your second question, if the function file is located within your path, MATLAB will find it when you call it. Call the function as you normally would. If the file is not within your current path, you can use addpath to add it to your path. Let me know if I misunderstood your question.

Sign in to comment.

Accepted Answer

Jon
Jon on 17 May 2022
I have been taking an approach where I try to keep the code in the app designer very simple and then define a class which provides all the complex functionality for the callbacks, accessing data etc.
So for example I define a handle class which I call GuiManager. I then assign an instance, guiManager, of this class as a property of the app.
All of the complicated functionality is then coded as methods of the GuiManager class. Within app designer if I want to call one of these functions, for example myMethod, e.g. within a callback, I just reference it as app.guiManager.myMethod(arg1,arg2,...)
Are you able to understand the approach I've just outlined? If you want further details please ask.
  3 Comments
Jon
Jon on 19 May 2022
I just coded up a small example similar to yours. It seems to work fine directly setting the label using a function in an external file. Please put both of these files in the same directory (or set the path to the directory where giveFive is) and try.
One thing you may want to check. The errors for the app designer may appear in the app designer window not on the command line. Maybe you have some small error in your code.
Jon
Jon on 23 May 2022
Glad you got it working, sounds like you had the right idea all along. I haven't thought about it too much, but I think there are probably some tradeoffs between the convenience of supplying the external function the entire app object, and the risks that this allows the external function access to too much. A little like a global variable. If it unintentionally does something to parts it shouldn't be touching it could be hard to trace what went wrong. So maybe sometimes it is better to just send the external program the data it needs and return a result and then let the app use the data.

Sign in to comment.

More Answers (1)

Inception
Inception on 18 May 2022
Edited: Inception on 18 May 2022
Thank you for your answers Kevin and Jon.
I made an example. Imagine you have this app.
Now, let's try to make Label become '5' when we press Button and Label2 become '6' when we press Button2. The two pushbutton event handler functions look like this.
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: Button
function ButtonPushed(app, event)
app.Label.Text = num2str(giveFive());
end
% Button pushed function: Button2
function Button2Pushed(app, event)
giveSix();
end
end
For Button we call a function giveFive() that simply returns the integer value 5.
%% file giveFive.m
function five = giveFive()
five = 5;
end
This, of course, works well as long as giveFive.m is within a directory MATLAB knows.
Now suppose we want to write a function that directly changes the label on the app. So we might create a file giveSix.m with the following defined function
%% file giveSix.m
function giveSix(app)
app.Label2.Text = '6';
end
This does not work. I don't get any errors in the command window, but also nothing happens with Label2 when I press Button2.
This is acutally my question. How can I manipulate app elements "outside" appdesigner? If I have a complex app I want to create separate *.m files to make the code within appdesigner more readable.
edit/
I partially found what I need. If I write within appdesigner
% Button pushed function: Button2
function Button2Pushed(app, event)
giveSix; % instead of giveSix()
end
and rewrite giveSix.m to
%% file giveSix.m
app.Label2.Text = '6';
then my problem is solved. But still I question if one can achieve this also within a function that is outside appdesigner.
  2 Comments
Walter Roberson
Walter Roberson on 18 May 2022
app is handle class. You can define get and set operations and put access restrictions on them.
Inception
Inception on 22 May 2022
Hi.
After some days I went through my code again and I spotted my mistake. I actually cannot explain why I had this problem as the solution is quite simple and absolutely within how functions are used in general.
Within appdesigner I have to call the external function giveSix() by writing
function Button2Pushed(app, event)
giveSix(app);
end
and not
function Button2Pushed(app, event)
giveSix();
end
No wonder why nothing happend on the GUI as app was empty since nothing was passed to giveSix().
Thank you for your help!

Sign in to comment.

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!