Running an m file by clicking on a pushbutton in GUI

조회 수: 11 (최근 30일)
Henry
Henry 2013년 6월 28일
댓글: Walter Roberson 2021년 1월 28일
Hiya,
I'm making a GUI using GUIDE and I want it to run a different file when i press a push button.
The file itself runs a few other files when it's run and gives an output of a series of variables with their values.
I was hoping I would be able to load and display these values in the GUI by running the file, and then be able to edit the values (still in the GUI).
Then, by pressing another pushbutton the GUI calls the file to calculate it's outputs using the updated variables and displays it.
I've tried using
run('myfile.m')
in the pushbutton callback, but the workspace isnt updated, so I'm not sure if it's actually being run or not.
alternatively I could call the GUI from the file, but I don't know how to do this either
Any help would be brilliant
Thanks
  댓글 수: 6
Amit Patankar
Amit Patankar 2021년 1월 28일
@Walter Roberson I am using a push button in App designer and in it's callback, I have written a Run command to run *.m files.
Just like when a *.m file is ran in command window of MATLAB and it loads all parameters into workspace, I expect that the push button should do it for me.
Walter Roberson
Walter Roberson 2021년 1월 28일
Well, it is loading the parameters into the workspace -- the workspace of the callback function.
As I wrote above:
Do not use run() to try to work with variables in the base workspace: use evalin('base') for that.

댓글을 달려면 로그인하십시오.

채택된 답변

David Sanchez
David Sanchez 2013년 6월 28일
To run a m-file from a GUI, just insert the name of the m-file in the pushbutton callback function. It works the other way around. To open a GUI from a m-file, just type the name of the GUI in the m-file.
If you want your GUI to display values calculated within the m-file, you could define variables as global in both the GUI and the m-file.
If what you want is to display/send values to the workspace, you could use
assignin
from both your GUI or you m-file.
type
help assigin
or
doc assigin
in the command window to get all the information about the assigin built-in function.
  댓글 수: 4
Henry
Henry 2013년 6월 28일
I've managed to solve this now, thank you for the help
Minhaz Bin Malik
Minhaz Bin Malik 2014년 6월 9일
Hello Henry, I am having the same problem like yours. Could you please share, how you eventually managed?

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Jan
Jan 2013년 6월 28일
You can obtain the results created by your function simply by catching them as outputs:
Result = myfile;
This is completely the same as for calling sin(0.1), where you will store the results in an output also.
Any kind of remote controlled assignments of variables cannot compete with this clean and clear method.
  댓글 수: 4
Walter Roberson
Walter Roberson 2019년 2월 10일
motevalizadeh comments
right
Margarita Diaz-Cortes
Margarita Diaz-Cortes 2020년 2월 13일
Your reply just made my day, thank you so much!

댓글을 달려면 로그인하십시오.


Tonghui Zhang
Tonghui Zhang 2019년 5월 9일
Hi guys,
I am confronted with a similar problem. When I use run command in my GUI, it works well. But after I generated an exetutable program(exe), the run function report that the specified m file cannot be found. When I use a constant path to the run function. It reports that the m script cannot be executed. But the problem is that I can run it very well in guide. Are there anyone can help me?
Thanks a lot
  댓글 수: 2
Walter Roberson
Walter Roberson 2019년 5월 13일
In another post we discussed that run is not permitted in compiled executables. You indicated that you were able to eval instead
Tonghui Zhang
Tonghui Zhang 2019년 5월 13일
Yeah,
This is a previous question.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by