Not necessarily private functions. I am also happy to do it with public functions.
How to properly structure code in appdesigner?
22 views (last 30 days)
Show older comments
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
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.
Accepted Answer
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
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
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.
More Answers (1)
Inception
on 18 May 2022
Edited: Inception
on 18 May 2022
2 Comments
Walter Roberson
on 18 May 2022
app is handle class. You can define get and set operations and put access restrictions on them.
See Also
Categories
Find more on Develop Apps Using App Designer in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!