Clear Filters
Clear Filters

Calling a function into a pushbutton in GUI

33 views (last 30 days)
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
Azzi Abdelmalek on 9 Mar 2013
Just call your function in the callback of your push button
  17 Comments
Azzi Abdelmalek
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
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'));

Sign in to comment.


Achchuthan Ganeshanathan
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
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
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 *

Sign in to comment.

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!