Calling a function into a pushbutton in GUI

37 次查看(过去 30 天)
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

回答(2 个)

Azzi Abdelmalek
Azzi Abdelmalek 2013-3-9
Just call your function in the callback of your push button
  17 个评论
Azzi Abdelmalek
Azzi Abdelmalek 2013-3-9
编辑:Azzi Abdelmalek 2013-3-9
Ok, then in a callback just write
% assign a value to h
h=100 % for example
Newfunction (h)
Azzi Abdelmalek
Azzi Abdelmalek 2013-3-9
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
% --- 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 个评论
Azzi Abdelmalek
Azzi Abdelmalek 2013-3-9
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
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 *

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by