How to execute a function upon pushing a button in a GUI constructed with GUIDE?
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I have created a GUI using the GUIDE interface. Users will input two values and push a button to run the program. I have created the two input values as global variables and defined them in the edit1_Callback and edit2_Callback as
FP = str2double(get(hObject, 'String')); and GP = str2double(get(hObject, 'String')); respectively.
The FP and GP values are located within a function BVP. I'm struggling to understand how to get the function to execute in the button's callback upon pushing the button. Any example code/advice would be useful. I haven't had success in finding an applicable example up to now.
0 Kommentare
Akzeptierte Antwort
Image Analyst
am 10 Dez. 2012
Gabrielle, try this code in your button's callback. Button tag is btnGo.
%=====================================================================
% --- Executes on button press in btnGo.
function btnGo_Callback(hObject, eventdata, handles)
% Retrieve the contents of the two edit boxes and convert to doubles.
FP = str2double(get(handles.edit1, 'String'));
GP = str2double(get(handles.edit2, 'String'));
% Then use them however you want.
You don't need to have anything at all in the callbacks for the edit functions. No need to retrieve the edit box contents in the callbacks for the edit functions. Only need to retrieve them in the callback for the button. I'm not sure what your BVP() function is. If you have another, non-callback function called BVP that you've written and inside which you need FP and GP, you can use the same code above, just be sure to pass handles into BVP() via the input argument list. Reply back if you still have uncertainties.
2 Kommentare
Image Analyst
am 10 Dez. 2012
Bearbeitet: Image Analyst
am 10 Dez. 2012
After you set the current axes with the axes() function, then you need to plot something into it. If you just call axes(handles.axes2) then you just shift focus to axes2 and nothing at all happens in axes1. Also, you either need to pass FP and GP into ViscousPumpBVP()
[x,y,yp] = ViscousPumpBVP(FP, GP);
or else pass handles into it and get FP and GP from within ViscousPumpBVP().
[x,y,yp] = ViscousPumpBVP(handles);
If you do it the latter way, then you don't need to get FP and BP from within brnGo_callback() since it's not used there and ViscousPumpBVP will get it anyway.
Your comment says
% Save user values as global variables
That is not true. With the code you have will have the FP and BP be just local variables, not global ones. You might check out the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Scope Variables and Generate Names finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!