필터 지우기
필터 지우기

Calling a function into a pushbutton in GUI

조회 수: 33 (최근 30일)
Achchuthan Ganeshanathan
Achchuthan Ganeshanathan 2013년 3월 9일
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
Achchuthan Ganeshanathan 2013년 3월 9일
% --- 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
Achchuthan Ganeshanathan 2013년 3월 9일
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에서 Interactive Control and Callbacks에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by