Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

Run m files by using GUI

Asked by Dogus Unlu

Dogus Unlu (view profile)

on 31 Jul 2011

I need to run m file from the push button by using GUI and enter some variables before the run.How can I apply these by GUI? There is a loop in the m file. Is that a problem for the GUI application? I will be appreciated with your answers. If there is a simple example, I will be so satisfied.

With best regards.


Dogus Unlu

Dogus Unlu (view profile)



2 Answers

Answer by Paulo Silva

Paulo Silva (view profile)

on 31 Jul 2011
Accepted answer

m files can be scripts or functions, scripts (group of MATLAB statements) you just run them by writing their name, functions you also execute by writing their name but you can also provide arguments and provide outputs.

Please refer to your MATLAB documentation

doc scripts %execute this to learn about scripts

Also this:

doc functions %execute this to learn about functions

No problems with loops if you know what your are doing, I can't help you more with just the details you provided.


Dogus Unlu

Dogus Unlu (view profile)

on 6 Aug 2011

@Jan:Thanks for all the answers, Jan. I have an addititonal problem.I can enter values and use the entered values in a simple function like yours(RAND(x,y)). But in my case, the entered values must be used in the m file. These entered values must be saved as a variable in the workspace. How can I do that?

With best regards.

Jan Simon

Jan Simon (view profile)

on 7 Aug 2011

@Dogus: There is usually no good reason to store entered variables in "the" workspace - btw. which workspace do you mean? Each function uses its own workspace...

Dogus Unlu

Dogus Unlu (view profile)

on 13 Aug 2011

@Jan: Thanks for all your answers. I have solved the problem. I have been trying to improve the code to be effectively used with the GUI.

Paulo Silva

Paulo Silva (view profile)

Answer by Jan Simon

Jan Simon (view profile)

on 31 Jul 2011

You describe the one and only use of a GUI. Any GUI is used to define values and start a program, otherwise it is not a GUI, but a picture or a message box.

I suggest to read Matt Fig's 41 GUI examples.

[EDITED] This is my small example:

A trivial example - save this as an M-file:

function myFirstGUI
ValueH = uicontrol('Style', 'edit', 'String', '0', ...
                   'Position', [10, 100, 100, 24]);
StartH = uicontrol('Style', 'PushButton', 'String', 'Start', ...
                   'Position', [10, 50, 100, 24], ...
                   'Callback', {@myFunction, ValueH});
function myFunction(ButtonH, EventData, ValueH)
S = get(ValueH, 'String');
Data = sscanf(S, '%g', 1);
plot(1:10, rand(1, 10) * Data);

[EDITED] End of my small example - @Dogus: Can you see it?


nurliana aqilah

nurliana aqilah (view profile)

on 12 Jun 2012

can anyone help me??i'm still cannot run mfile in gui..please..i don't understand how to use the command. i try to use open('filename') but it just open it not run it automatically

deniel munthe

deniel munthe (view profile)

on 12 Jun 2012

i thing if you just want to run mfile in gui, you just call the function.m; it's will automaticly run your mfile


Fa' (view profile)

on 19 Jul 2012

Hi all!! I have to run mfile in gui too. I don't understand where I have to call the function, probably in the callback of the button related?

Jan Simon

Jan Simon (view profile)

Contact us