現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
Calling a function into a pushbutton in GUI
9 ビュー (過去 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!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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 (한국어)
