How to know which push button clicked, to link it with the data retrieval?

I would like to connect a push button ID to the input data retrieval. The input data is in cell array. I am using GUIDE to build the GUI.
I have 3 push buttons (for example, push button 1, 2, and 3) that are linked to the same GUI window (for example, GUI1) when they are clicked. Let's say that I click push button 1 and GUI1 pops up. At GUI1, the opening function has commands to retrieve the input data{1}. If push button 2 is clicked, the input data retrieved should be input data{2}.
Any idea how can I know which push button clicked to have the GUI1 retrieve the correct data input?
Thanks a lot! :)
Elizabeth

1 commentaire

I think what I meant is how does the GUI1 know which pushbutton clicked by the user? Is there any way to send this information to GUI1?

Connectez-vous pour commenter.

 Réponse acceptée

Adam
Adam le 10 Mai 2016
Each pushbutton has its own callback by default so you already know which one was pressed.
Obviously if you want to do effectively the same code for each of them you may want to either call a common function from these callbacks or override the default callbacks by giving them all the same callback. In the latter case the 'hObject' argument is the pushbutton that you pressed and its 'tag' property will identify it for you (I tend to give all my buttons sensible tag names so I remember what they are).

4 commentaires

Hello Adam. Thanks for answering. I still don't get it what does it mean by giving them all the same callback?
You can edit the callback function in GUIDE and assign any function you want so long as it has the right signature, so you can give all pushbuttons the same callback if you wish. It isn't necessary though - you can just put the common code in some other function and call that from each of the individual callbacks, with each one passing in some variable that indicates which pushbutton was pressed.
You can launch a new GUI with arguments as e.g.
myGUI = GUI1( someArg );
so each pushbutton callback can pass an argument that you can interpret to know which pushbutton it was. Personally I would make this argument something that is related to the purpose of the pushbutton, not just an id of the pushbutton itself as that is just an implementation detail. The pushbutton string property would work fine for this since it should have a sensible user context. So something like
function myPushButtonCallbac( handles, evtData, hObject )
GUI1( hObject.String );
Hi Adam, ok, let me try! thanks! :)
Hey thanks! Giving the input to the GUI1 so far helps!

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by