Using App Designer, how do I execute .m files in the main workspace?
48 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Rik
2019 年 6 月 8 日
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.
回答 (1 件)
edison ivan cabrera parco
2020 年 3 月 16 日
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 件のコメント
Aron Horvath
2021 年 3 月 3 日
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
2021 年 3 月 3 日
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)
参考
カテゴリ
Help Center および File Exchange で Develop Apps Using App Designer についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!