How to execute a function upon pushing a button in a GUI constructed with GUIDE?

7 vues (au cours des 30 derniers jours)
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.

Réponse acceptée

Image Analyst
Image Analyst le 10 Déc 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 commentaires
Gabrielle
Gabrielle le 10 Déc 2012
Thank you, Image Analyst for your quick and helpful feedback! To clarify your suggestion for passing my BVP function into the callback of the button:
My function is [x,y,yp]=BVP() which runs a separate series of functions within it and then plots an output. To execute this function on the button press would I set up something like this?
% --- Executes on button press in btnGo.
function btnGo_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Save user values as global variables
FP = str2double(get(handles.edit1, 'String'));
GP = str2double(get(handles.edit2, 'String'));
%Call function and plot results
[x,y,yp] = ViscousPumpBVP()
axes(handles.axes1)
axes(handles.axes2)
% code
end
When I'm running this I can execute my function, but it still displays my plots in separate pop-up windows as if I were running a script.
Image Analyst
Image Analyst le 10 Déc 2012
Modifié(e) : Image Analyst le 10 Déc 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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Scope Variables and Generate Names dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by