Calling a function into a pushbutton in GUI

I have a button in my GUI which should run a function when pressed.I have made the function externally and I want it call it in the pushbutton. How can I go by doing that. could anyone please help me

Respuestas (2)

Azzi Abdelmalek
Azzi Abdelmalek el 9 de Mzo. de 2013

0 votos

Just call your function in the callback of your push button

17 comentarios

% --- Executes on button press in Calculate.
function Calculate_Callback(hObject, eventdata, handles)
% hObject handle to Calculate (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
Do I write this after the above lines to call my function?
[filename, pathname] = uigetfile({'*.*'},'Open Directory');
if isequal(filename,0) || isequal(pathname,0)
return
end
Azzi Abdelmalek
Azzi Abdelmalek el 9 de Mzo. de 2013
Yes, write whatever you want
the filename would be in my case 'Newfunction.m'
Would the path name be C/user/... ?
Azzi Abdelmalek
Azzi Abdelmalek el 9 de Mzo. de 2013
Your function should be in the same folder as your gui
% --- Executes on button press in Calculate.
function Calculate_Callback(hObject, eventdata, handles)
% hObject handle to Calculate (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[Newfunction.m,Users/Achchu/Documents/MATLAB/Newfunction] = uigetfile({'*.*'},'Open Directory');
if isequal(Newfunction.m,0) || isequal(Users/Achchu/Documents/MATLAB/Newfunction,0)
return
end
That is what I've written but there it comes up as an error. Yes my function and GUI are both under my 'MATLAB' Folder
What is this ?
[Newfunction.m,Users/Achchu/Documents/MATLAB/Newfunction] = uigetfile({'*.*'},'Open Directory');
What do you want to do?
I don't actually know tbh. I found the steps on a wesbite. All I want to do it open my other function and run it when i press the pushbutton. Do you think i need to get rid of that line?
Azzi Abdelmalek
Azzi Abdelmalek el 9 de Mzo. de 2013
This line is not correct, and I think, instead of trying some code from the web, you should learn how to use functions. You also need to learn basics of Matlab
I do use matlab at uni but i only normally deal with simulink I am afraid
Ok, If you want to call a function, for example, If your function is
function y=filname(a,b)
%your code
To call it from you pushbutton calbback, just write, for example
a=10;
b=4
y=filname(a,b)
% --- Executes on button press in Calculate.
function Calculate_Callback(hObject, eventdata, handles)
% hObject handle to Calculate (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
Callback 'Newfunction';
Would that suffice do you reckon
Azzi Abdelmalek
Azzi Abdelmalek el 9 de Mzo. de 2013
Newfunction is a function or a script?
It's another function I created in editor in matlab
Azzi Abdelmalek
Azzi Abdelmalek el 9 de Mzo. de 2013
If it's a function, then what are its inputs and outputs argument? can you post this function?
Achchuthan Ganeshanathan
Achchuthan Ganeshanathan el 9 de Mzo. de 2013
Editada: Azzi Abdelmalek el 9 de Mzo. de 2013
function Newfunction (h)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
if 105 < h && h < 120
warningMessage = sprintf('Warning: your velocity = %f\nYou may stall!', h);
uiwait(warndlg('Approaching Stall'));
elseif 100<h && h <105;
warningMessage = sprintf('Warning: your velocity = %f\nYou may stall!', h);
uiwait(warndlg('Close to stall'));
elseif h <100
warningMessage = sprintf('Warning: your velocity = %f\nYou may stall!', h);
uiwait(warndlg('STALL'));
end
that is the function. i haven't defined an input in this function because *in my scenario, my GUI requires an input from the user and when i type in a number and press calculate the pushbutton which is 'calculate' should look at the user input for instance it is the variable 'h'and perform the function above and display the warning messages accordingly*
<<Stalllllll>>
Azzi Abdelmalek
Azzi Abdelmalek el 9 de Mzo. de 2013
Editada: Azzi Abdelmalek el 9 de Mzo. de 2013
Ok, then in a callback just write
% assign a value to h
h=100 % for example
Newfunction (h)
Look, you have nothing to write in your editbox callback, the action is taken when the user click on pushbutton, then in your pushbutton callback write
h=str2double(get(handles.Speed_Callback,'string'));

Iniciar sesión para comentar.

% --- Executes on button press in Calculate.
function Calculate_Callback(hObject, eventdata, handles)
% hObject handle to Calculate (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
Callback % assign a value to h
h=100 % for example
Newfunction (h)
I have done as you said, and the function seems to work. but when i press the pushbutton matlab gives me an error
??? Undefined function or variable 'Callback'.
Error in ==> Stallfunction>Calculate_Callback
at 102
Callback % assign a value to h
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> Stallfunction at 42
gui_mainfcn(gui_State, varargin{:});
Error in ==>
@(hObject,eventdata)Stallfunction('Calculate_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
>>

6 comentarios

What Callback is doing in your code? remove the line
Callback % assign a value to h
from your code
that has worked thank you. currently its giving me an output of 100 as that's the value i've given for 'h'.
if i were to associate 'h' with my textbox in GUI which is where i input my speed values then
Could I use
h= function Speed_Callback(hObject, eventdata, handles)
to make 'h' the value that is entered by the user at different occasions. the 'function speed_......' is the code for my text box
Azzi Abdelmalek
Azzi Abdelmalek el 9 de Mzo. de 2013
Achchuthan, I suggest that you read the help of guide Matlab.
the one solution from matlab guide's is to use 'string'
Use an edit box, where the user can put the value of h, then in your pushbutton callback write
h=str2double(get(handles.edit1,'string'))
I have already created the box as an edit box and its named as Speed
function Speed_Callback(hObject, eventdata, handles)
% hObject handle to Speed (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of Speed as text
% str2double(get(hObject,'String')) returns contents of Speed as a double
h=str2double(get(handles.Speed_Callback,'string'));
% --- Executes during object creation, after setting all properties.
function Speed_CreateFcn(hObject, eventdata, handles)
% hObject handle to Speed (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in Calculate.
function Calculate_Callback(hObject, eventdata, handles)
% hObject handle to Calculate (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
h= Speed_Callback(hObject,eventdata,handles) % for example
Newfunction (h)
*The 'SPEED' refers to my edit box where the users input values *

Iniciar sesión para comentar.

Categorías

Más información sobre Interactive Control and Callbacks en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 9 de Mzo. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by