Reputation: 105
I am just starting to learn about push buttons and i am stuck. I have two pop up menus. When the user selects the push button. the total from each pop up menu selection is returned to the user. I got the values from the pop up menu but I dont know how to return it after the push button is executed. Any help is appreciated
function pushbutton1_Callback(hObject, eventdata, handles)
math=0;
data1 =get(handles.popupmenu1, 'Value') %processing data from first pop up menu
if data1== 1
math=1
elseif data1 == 2
math=4
end
data2=get(handles.popupmenu2, 'Value') %processing data from second pop up menu
if data2==1
math=math + 5;
end
% I tabulated math which is some number. I want to return it back to the user
in a text outside of the button.
Upvotes: 1
Views: 579
Reputation: 4732
A more elaborate, but more powerful way would be to create your own handle class.
Doing that you could add callbacks to custom data. I did an example here.
Upvotes: 1
Reputation: 1832
There is another way then returning the value: You pass and store it within the guidata-structure. Search for guidata in the docs for detailed information. One example from the docs:
function My_Callback()
% ...
% Get the structure using guidata in the local function
myhandles = guidata(gcbo);
% Modify the value of your counter
myhandles.numberOfErrors = myhandles.numberOfErrors + 1;
% Save the change you made to the structure
guidata(gcbo,myhandles)
Short explanation how to do it:
1. get the data by myhandles=guidata(handle_of_the_figure)
2. add/modify data, like myhandles.Test = 123
3. dont forget to save the changes, otherwise they will "just disappear"-> use guidata(handle_of_the_figure,myhandles)
4. to test it, just load the guidata afterwards within another function and look for the changes!
EDIT
while re-reading your question, it came to my mind, that you perhaps just want a value calculated within your callback to be displayed somewhere else. for example, if you want the value showing up in an text-edit uicontrol, you can use:
set(HandleOfTheTextEdit, 'String', num2str(mat))
Upvotes: 2