Using App Designer, how do I execute .m files in the main workspace?

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

David - from the line of code
run(RAWDatabase)
what is assigned to RAWDatabase? Is it the path to the file (i.e. 'C:\Users\path\RAWDatabase.m')? If it is, why create a variable name that has the same name as the function? (That seems confusing and prone to errors.)
As for the there are a LOT of variables that get passed around from one .m files to the next, are these m-files scripts or functions? If the former, why not convert them to the latter so that the outputs of one function can be used as the input to another?
Can you clarify whether your .m files are scripts or functions?
RAWDatabase is the name of the .m file, RAWDatabase.m located at that path. I am not treating RAWDatabase as a variable.
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:
  1. use run('RAWDatabase')
  2. use RAWDatabase
  3. convert the script to a function
The last solution is by far the best choice in the long run.
Thank you, I'll try that. I did not realize that I was treating it as a variable instead of a script.

Sign in to comment.

Answers (1)

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

Yes, although you should be using functions instead of scripts.
Also, this isn't an answer but a comment.
Ok thank very much. I want use this script because I don´t know how read a file .nmea and then write this information in a txt file.
Changing a script to a function isn't hard. Any basic Matlab toturial should teach you how to make functions. The main point you should remember is that scripts tend to have their inputs and outputs hardcoded. The only thing you need to do is making sure you turn all those hardcoded inputs into input arguments. In this case that would probably mean the path and filename of your .nmea and .txt files.
Happy to help. If you have issues solving this problem have a read here and here and post a separate question. Feel free to post a comment here with a link to your new question.
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 ?
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)

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Asked:

on 2 Jun 2019

Commented:

Rik
on 3 Mar 2021

Community Treasure Hunt

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

Start Hunting!