How do I call on a function created outside of the App Editor?

10 views (last 30 days)
Not sure if I am describing my problem correctly, but what I would like to do is call on a script/function I have created when a user presses a button in my app.
For instance, I have a script that will take a use inputted image, run analysis on it, and output the results. Everything works fine when it is run as a script, and so was hoping to port this over to an app, where the use inputs the image, and clicks an "Analyze" button, which would run the script I have created.
This is my first time trying to create an app and I find the documentation very poor. I can't find any information regarding this online.

Answers (2)

Prem Kumar Tiwari
Prem Kumar Tiwari on 28 Sep 2018
Hopefully you are using App Designer to create the GUI. You can refer to Call Backs for GUI in Matlab documentation and related to learn more on how to add callbacks to the buttons.
Stephen23 on 28 Sep 2018
Edited: Stephen23 on 28 Sep 2018
"how does the app recognize where the script will be located?"
It can't. You have to tell it.
It is not possible to save a script/function anywhere and expect MATLAB (or any other application) to find it within a finite (or reasonably short) length of time. Applications, like MATLAB, do not just magically know everything that is saved everywhere on a computer's harddrive (which these days also includes all kinds of links and clouds and who knows what else...). Even Windows, when it tries to keep track of all user documents for indexing purposes, has to create a huge database, which takes time to trawl through ... this is simply not feasible for an application like MATLAB: call a function, then wait thirty minutes while the entire harddrive gets searched... and then finally you get the output from max.
This is why applications install all of their files into just a few folders in program files (on Windows) and some user folders. Then, within those directories, the structure remains the same, so they can use relative paths to access everything, just where it should be. They don't look everywhere.
And for most programming languages, like MATLAB, functions/scripts have to be on some kind of search path in order for them to be found automatically by the application. The MATLAB Search Path is easy to change, so if your user wants to put their files in a highly unusual location, then they can do this. More power to them.
You have the choice of letting the user set/choose a path (e.g. uigetdir) as the root directory for where they save all of these files (possibly including subdirectories, so you can use relative paths or private folders). Or you can tell them to always put the files into the same location, e.g. C:\mycode, and then add that path automatically at the start of your first script. They might not like it, but it would work.
Or you could use a configuration file, where the user has to specify all of the path to the data and functions/scripts. Configuration files are neat and intuitive (if well designed).
Probably about now you will start to realize that there is no simple answer... because there is no simple answer. At some point you have to tell any application (e.g. MATLAB) where the files are. How you do that, e.g. with a user defined root directory or a fixed location, depends on you and your users and your use case and their write permissions and their sys-admins and many other things that we know nothing about.
At some point you have to trust your users that they can save your script into their MATLAB directory, and can actually use MATLAB. Creating some complex code that automagically installs and fiddles with the Search Path might sound great, but unless you have written a toolbox with thousands of files, this would just take lots of your time, and not bring any real benefit.
"For instance, if I compile the app, and send it to someone else..."
Compiled code is a totally different kettle of fish. Search this forum to know more, e.g.:

Sign in to comment.

Stephen23 on 28 Sep 2018
Edited: Stephen23 on 28 Sep 2018
"How do I actually point the App to the script located on my computer?"
Simple, just change the MATLAB Search Path., because that is exactly what the MATLAB Search Path is for:
The Search Path is just a list of directories to tell MATLAB where you have scripts/functions/classes saved, so that it can look for them there. Once you add those directories to the path:
then you can just call those scripts/functions normally. Programming languages require a search path because looking everywhere for functions or scripts is
  1. impossible
  2. very slow
"Is there a way to store the scripts within the app itself??"
Probably you could convert them to functions and include them in the same file, but this is unlikely to be a good idea: keeping functions and scripts in functionally distinct categories and directories makes management and maintenance much easier. I doubt that it would help you to put everything into one big file.
Stephen23 on 28 Sep 2018
Edited: Stephen23 on 28 Sep 2018
@Dennis: I have never used GUIDE or AppDesigner, so I have no idea.
I write my own GUI code, which does not need to be edited, and works from any directory on the MATLAB Search Path. I find that easier.

Sign in to comment.


Find more on File Operations 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!