Calling a function into a pushbutton in GUI
20 次查看(过去 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 (한국어)