Using App Designer, how do I execute .m files in the main workspace?
48 views (last 30 days)
Show older comments
Hi, I'm currently developing an app and finished the backend part. I thought it would be super easy to add the frontend part by just integrating the .m files directly into an App Designer file, but this has proven to be extremely frustrating. All I need is for the app to execute a series of .m files when a button is pressed.
Here's what I tried:
Writing the cd command within app designer under the callback function for a specific button to change the directory to the location of said .m files
This didn't work because: There are a LOT of variables that get passed around from one .m files to the next, so I was expecting that these variables would share their data, but I was quickly mistaken. I would literally need to declare every single variable as "public" ahead of time.
Writing the run and assignin command within app designer as suggested here: https://www.mathworks.com/matlabcentral/answers/80530-running-an-m-file-by-clicking-on-a-pushbutton-in-gui
This didn't work because: MATLAB hates me. Here's the cryptic error it gave me:
Attempt to execute SCRIPT RAWDatabase as a function:
C:\Users\path\RAWDatabase.m
Error in app2/ExecuteButtonPushed (line 62)
run(RAWDatabase)
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 335)
Error while evaluating Button PrivateButtonPushedFcn.
Any suggestions? Also, running MATLAB 2019a.
6 Comments
Rik
on 8 Jun 2019
Yes, you are. This is what is happening:
run(RAWDatabase)
This is not running a script RAWDatabase.m, but it is evaluating RAWDatabase. This means it has to either be a variable or a function. When Matlab tries running RAWDatabase as a function, it discovers it isn't a function but a script.
You have multiple options:
- use run('RAWDatabase')
- use RAWDatabase
- convert the script to a function
The last solution is by far the best choice in the long run.
Answers (1)
edison ivan cabrera parco
on 16 Mar 2020
So.. I undestand that if I write "run(´path of my script`)" in the code of appdesigner, I can execute this script for example If I use this in a callback function. Is this true?. Thank you
7 Comments
Aron Horvath
on 3 Mar 2021
And what if I have to use a function inside a nother function like this one
rk_dx('fx',t0,h,tf,x0,w)
where fx:
function dXdT = fx(x,w)
global c
dXdT(1) = x(1) - x(1)*x(2)-c(1)*x(1)*w;
dXdT(2) = -x(2) +x(1)*x(2)-c(2)*x(2)*w;
and rk_dx is a basic runge-kutta. Should I use the run command like this:?
x1=run(rk_dx('fx',t0,h,tf,x0,w));
I'm asking because when I run this rk_dx function from a script it's working but when I'm calling from app designer I get the following error:
Index exceeds the number of array elements (0).
Error in fx (line 6)
dXdT(1) = x(1) - x(1)*x(2)-c(1)*x(1)*w;
Error in rk_dx (line 24)
k1 = h*feval(f,x,w(j))'
Should I post my whole function or is there something what I have missed ?
Rik
on 3 Mar 2021
You should not hijack this thread with your question.
'fx' is not a function, it is a char array.
Are you sure you are calling your function with the exact same paramters in both situations? Because a function should generally not behave differently.
Your function uses a global variable. Have you checked if any other function might be using the same name? (this is why you shouldn't be using global variables (especially with such short names), they are a pain to debug)
See Also
Categories
Find more on Develop Apps Using App Designer in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!