이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
Calling a function into a pushbutton in GUI
조회 수: 33 (최근 30일)
이전 댓글 표시
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
2013년 3월 9일
Just call your function in the callback of your push button
댓글 수: 17
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)
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
Achchuthan Ganeshanathan
2013년 3월 9일
the filename would be in my case 'Newfunction.m'
Would the path name be C/user/... ?
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)
[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
2013년 3월 9일
What is this ?
[Newfunction.m,Users/Achchu/Documents/MATLAB/Newfunction] = uigetfile({'*.*'},'Open Directory');
What do you want to do?
Achchuthan Ganeshanathan
2013년 3월 9일
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
2013년 3월 9일
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
2013년 3월 9일
I do use matlab at uni but i only normally deal with simulink I am afraid
Azzi Abdelmalek
2013년 3월 9일
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
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 'Newfunction';
Would that suffice do you reckon
Azzi Abdelmalek
2013년 3월 9일
If it's a function, then what are its inputs and outputs argument? can you post this function?
Achchuthan Ganeshanathan
2013년 3월 9일
편집: Azzi Abdelmalek
2013년 3월 9일
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
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
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
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
2013년 3월 9일
What Callback is doing in your code? remove the line
Callback % assign a value to h
from your code
Achchuthan Ganeshanathan
2013년 3월 9일
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
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
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 Center 및 File Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기
태그
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- 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)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)