I have two push buttons in my MATLAB GUI. I am trying to recognize a push button in button1's callback function and do something with regard to which button was pressed. I have tried to use button group and put all my buttons in that group. It seems as if there is no code when any of these push buttons is clicked. Why?
Here is my code:
function uibuttongroup1_SelectionChangeFcn(hObject,eventdata)
switch get(eventdata.NewValue,'Tag') % Get Tag of selected object.
case 'notSimul'
disp('notSimul clicked')
case 'simul'
% Code for when radiobutton2 is selected.
case 'stopTest'
% Code for when togglebutton1 is selected.
case 'start'
% Code for when togglebutton2 is selected.
% Continue with more cases as necessary.
otherwise
% Code for when there is no match.
end
If I understand your question correctly that you placed a push button in a button group, the answer is it would not work because button group is supposed to be composed of only toggle button and radio button. When I tried putting a push button in a button group, nothing would happen, just as you described.
Related
How do I mimic a button click in matlab?
Simply excecuting the callback function doesn't work since within its callback it uses the gcbo command and I cannot alter the excecuting function. I furthermore would not like to shadow gcbo for obvious reasons.
In case it matters I look for a solution which works on matlab R2012a.
you can try calling the java.awt.Robot class, for example.
robot = java.awt.Robot;
pause(2) % wait some time
robot.keyPress (java.awt.event.KeyEvent.VK_ENTER); % press "enter" key
robot.keyRelease (java.awt.event.KeyEvent.VK_ENTER); % release "enter" key
read more about GUI automation using a Robot here...
I'm not sure this will work on Matlab R2012a, but it does on later versions.
gcbo only contain the button handle. If you have (or can retrieve/find) the button handle, just call the function callback with the button handle as first argument and an empty variable as the second argument.
something looking like that:
button_callback( buttonHandle , [] ) ;
The callback will not make any difference between gcbo or the button handle and will function exactly the same.
If you do not have the button handle in the first place, you can try to find it with findobj:
buttonHandle = findobj(gcf,'Style','PushButton','String','The Button Text')
Depending on how the GUI was built/defined, then handle visibility may not be immediately apparent, in which case you can search deeper with findall:
buttonHandle = findall(gcf,'Style','PushButton','String','The Button Text')
or may be the handle was nicely saved in the guidata structure:
handles = guidata(gcf) ;
and search the structure for what may be your button handle.
Note: in the last 3 examples above, make sure the GUI figure which contains the button has the focus before you call gcf, or better yet, replace gcf by the actual figure handle.
I have two radio buttons in my interface. I also have some pushbuttons. When ever I click a pushbutton I want it to call a function according to the selected radio button.
I tried adding the function given below
function rotation_SelectionChangeFcn(hObject, eventdata, handles)]
Tag = get(hObject, 'Tag');
disp(Tag);
But nothing is coming up when I change the selection. I want to know whether there is any mistake in the way I implemented the code or is there a better way of doing this?
Whenever you use radiobuttons you might want to regroup them in button groups, then it's quite easy to play with radio buttons, and you make sure that only 1 radio button per group gets selected at any time.
For radio buttons in button groups, you want to use the following:
get(eventdata.NewValue,'Tag')
to get the tag of the new value just being selected. You can also use OldValue as well if you want.
In the callback of your pushbutton, you can query whether a radio button is activated with its 'value', i.e. 1 if it is selected.
StateRadioButton = get(handles.RadioButton1,'Value'); %assuming the tag is "RadioButton1".
The hObject property is particular for the specific callback in which it is used, otherwise you need to use the handles structure to access elements from other functions.
I implemented various uimenus in my uitable but there appears a very annoying behaviour.
function createUItable
h = figure
...
uimenu(h,'Label','MenuButton','Callback',#someAction)
end
%---------
function someAction(~,~)
%some action
end
But after executing the callback function, the menu button remains pressed and highlighted and not even that, when I slide over the next menu button, this one is triggered also!
This behaviour was also described at Matlab Central, but without solution.
I tried the suggested:
function someAction(~,~)
%some action
set(gcbo,'Enable','off')
drawnow
set(gcbo,'Enable','on')
end
which does not change anything. set(gcbo,'Enable','off') alone would solve the sliding problem, but also disables the whole button, what I don't want.
I also tried to use the 'Checked','Visible' and 'Interuptible' property without success.
This problem must be known, any hints?
I also thought about using uicontrol instead of uimenu and use a pushbutton, but I don't get it work.
Edit: when I put my menubutton into a submenu it works perfect:
button = uimenu(h,'Label','MenuButton');
uimenu(button,'Label','MenuButton','Callback',#someAction)
Edit2:
A pushbutton works also, but how could I place it into the menubar?
I guess MATLAB implementation is this way, because setting a callback at the top-level menu is very odd.
Naturally in GUI's (not only MATLAB), when you click the top-level menu (like "File", "Edit", etc.) the standard behaviour is, that a submenu pops open rather than an immediate action being executed.
So you should only use the top-level callback to e.g. dynamically create/modify the associated submenus.
I think there are two alternatives to go:
1) If you'd like to stick to that manner (one, always-visible button-like element), then you should rather use the toolbar via a uipushtool:
hToolbar = uitoolbar(parentFigure);
uipushtool(hToolbar, 'ClickedCallback', #someAction);
This does not have the 'Label' property though, so you'll have to work with 'CData' and may be a 'TooltipString'.
2) Create a top-level menu that contains your actual action-menu:
topMenu = uimenu(parent, 'Label', 'Actions');
uimenu(topMenu, 'Label', 'MenuButton', 'callback', #someAction)
From the general point of view on GUI design, both alternatives have the benefit of being the more commonly used style, thus being more intuitive to any user.
I found an interesting work-around for this problem while keeping the callback in the TOP menu. Turns out that using the uistack function released focus from the menu item so in the top-level menu callback, I placed
uistack(hObj,'down');
uistack(hObj,'up');
drawnow;
Which does nothing to the actual ordering but releases the menu items' focus.
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.
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))
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.
I have created a button group with four radio buttons and a push button using guide.
There are four functions, one for each radio button written separately.
How do you to call these functions from respective radio buttons.
When a push button is pressed, the function associated with the active radio button should execute.
A solution for the Button Group Callback: SelectionChangeFCN
Use the Selection Change callback property (right click on the Button Group and select View Callbacks->SelectionChangeFcn) of the uipanel. The eventdata argument contains the handles to the current and previously selected radiobutton. The eventdata argument is a structure with the following fields:
EventName
OldValue
NewValue
So, depending on the value of eventdata.NewValue; for example
function uipanel1_SelectionChangeFcn(hObject,eventdata,handles)
...
newButton=get(eventdata.NewValue,'tag');
switch newButton
case 'radiobutton1'
% code for radiobutton 1 here
case 'radiobutton2'
% code for radiobutton 2 here
...
end
...
A solution for the push button callback
The callback for your push button could have something along the lines of
function button1_Callback(hObject,eventdata,handles)
h_selectedRadioButton = get(handles.uipanel1,'SelectedObject');
selectedRadioTag = get(h_selectedRadioButton,'tag')
switch selectedRadioTag
case 'radiobutton1'
case 'radiobutton2'
...
end
I also refer you to the MATLAB documentation for more information on Handle Graphics and building graphical user interfaces.
Crash course on GUI's starts... now:
If you're using guide, then when you save your figure mygui.fig, the M-file should be automatically generated as mygui.m. Open up mygui.m and enter your code under the radio button callback function. Any variables that you want initialized when you start the program should be defined under the opening function. Make sure you update the handles structure at the end of each callback, with the command guidata(hObject,handles).
For example, if you wanted two mutually exclusive radio buttons (when you select one, the other de-selects, or when you de-select one, the other is selected), enter the following code under their callbacks:
function radiobutton1_Callback(hObject, eventdata, handles)
if get(handles.hObject,'Value')
set(handles.radiobutton2,'Value',0)
else
set(handles.radiobutton2,'Value',1)
end
guidata(hObject,handles);
and
function radiobutton2_Callback(hObject, eventdata, handles)
if get(hObject,'Value')
set(handles.radiobutton1,'Value',0)
else
set(handles.radiobutton1,'Value',1)
end
guidata(hObject,handles);
And initialize radio button one to be selected under the opening function:
set(handles.radiobutton1,'Value',1)
set(handles.radiobutton2,'Value',0)