Calling a function from m.file into a pushbutton in GUI

Hi,
I have a m.File with the a function "myfun(input)" and a GUI with a pushbutton. I want to run that mfile function by pressing the pushbutton. I don´t know how to implemente it correctly into the pushbutton-function.
Any help would be great!
This one doesn´t work :/:
function generatebtn_Callback(hObject, eventdata, handles)
input=xyz;
myfun(input);
Thank you:)

5 Commenti

What happens when you run this? What does the function myfun do?
Hello,
your code should actually work. Make sure Matlab can find the m.file "myfunction.m" by adding the corresponding folder to Matlab's search path or adding your function file to the GUI execution folder. If it still does not work, try to figure out what Matlab's error message is tellng you.
Good luck!
Thanks for the answers.
With the following simple example it also doesn´t work:
m.File:
function myfun(x)
msgbox(x);
end
_________
GUI:
function pushbutton1_Callback(hObject, eventdata, handles)
myfun('Hello');
______
Error:
Undefined function or variable 'myfun'.
Error in untitled1>pushbutton1_Callback (line 81)
myfun('HI');
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in untitled1 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)untitled1('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
____________
My m.file is also in the same folder as the GUI file, so that one shouldn´t be the problem.
Probably an obvious question, but is your .m file called myfun.m ?
I´m new to Matlab ...I thought with myfun I would only call the m.File to run and not the function itself(with its inputs).
Thank you and sorry for the stupid question :D

Accedi per commentare.

Risposte (1)

To open .m files with a pushbutton use this:
% --- Executes on button press in pushbutton
function pushbutton_Callback(hObject, eventdata, handles)
strM_File = ('C:\Users\Desktop\Matlab\YourMFileNameHere.m'); %The folder which contains your .m file
open (strM_File);

Categorie

Scopri di più su Interactive Control and Callbacks in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by