You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Calling a function into a pushbutton in GUI
18 views (last 30 days)
Show older comments
Achchuthan Ganeshanathan
on 9 Mar 2013
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
Answers (2)
Azzi Abdelmalek
on 9 Mar 2013
Just call your function in the callback of your push button
17 Comments
Achchuthan Ganeshanathan
on 9 Mar 2013
% --- 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
on 9 Mar 2013
Yes, write whatever you want
Achchuthan Ganeshanathan
on 9 Mar 2013
the filename would be in my case 'Newfunction.m'
Would the path name be C/user/... ?
Azzi Abdelmalek
on 9 Mar 2013
Your function should be in the same folder as your gui
Achchuthan Ganeshanathan
on 9 Mar 2013
% --- 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
Azzi Abdelmalek
on 9 Mar 2013
What is this ?
[Newfunction.m,Users/Achchu/Documents/MATLAB/Newfunction] = uigetfile({'*.*'},'Open Directory');
What do you want to do?
Achchuthan Ganeshanathan
on 9 Mar 2013
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
on 9 Mar 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
Achchuthan Ganeshanathan
on 9 Mar 2013
I do use matlab at uni but i only normally deal with simulink I am afraid
Azzi Abdelmalek
on 9 Mar 2013
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)
Achchuthan Ganeshanathan
on 9 Mar 2013
% --- 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
on 9 Mar 2013
Newfunction is a function or a script?
Achchuthan Ganeshanathan
on 9 Mar 2013
It's another function I created in editor in matlab
Azzi Abdelmalek
on 9 Mar 2013
If it's a function, then what are its inputs and outputs argument? can you post this function?
Achchuthan Ganeshanathan
on 9 Mar 2013
Edited: Azzi Abdelmalek
on 9 Mar 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
on 9 Mar 2013
Edited: Azzi Abdelmalek
on 9 Mar 2013
Ok, then in a callback just write
% assign a value to h
h=100 % for example
Newfunction (h)
Azzi Abdelmalek
on 9 Mar 2013
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'));
Achchuthan Ganeshanathan
on 9 Mar 2013
% --- 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 Comments
Azzi Abdelmalek
on 9 Mar 2013
What Callback is doing in your code? remove the line
Callback % assign a value to h
from your code
Achchuthan Ganeshanathan
on 9 Mar 2013
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
on 9 Mar 2013
Achchuthan, I suggest that you read the help of guide Matlab.
Achchuthan Ganeshanathan
on 9 Mar 2013
the one solution from matlab guide's is to use 'string'
Azzi Abdelmalek
on 9 Mar 2013
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'))
Achchuthan Ganeshanathan
on 9 Mar 2013
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 *
See Also
Categories
Find more on Interactive Control and Callbacks in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)