Why does button group not work?

2 vues (au cours des 30 derniers jours)
Tim Mottram
Tim Mottram le 20 Juil 2012
Hi Guys,
I am making a GUI which, to all intents and purposes, plots some data. I wanted a set of radio buttons to change the plot colour. To make sure only one colour was selected I used a button group. When I ask to see the radio button call back I am told the uiButtonGroup (plotColour) is managing the call back. This is my button group SelectionChangeFcn code :
function plotColour_SelectionChangeFcn(hObject,eventdata)
switch get(hObject,'Tag') % Get Tag of selected object.
case 'Blue'
setappdata(handles.figure1,'Colour','blue')
case 'Red'
setappdata(handles.figure1,'Colour','red')
case 'Green'
setappdata(handles.figure1,'Colour','green')
case 'Yellow'
setappdata(handles.figure1,'Colour','yellow')
case 'Black'
setappdata(handles.figure1,'Colour','black')
case 'cyan'
setappdata(handles.figure1,'Colour','cyan')
case 'Magenta'
setappdata(handles.figure1,'Colour','magenta')
otherwise
end
This is the section of code which plots the data:
global noFiles
global plotLength
dataStore = getappdata(handles.figure1 , 'dataStore');
f = getappdata(handles.figure1 , 'f');
plotColour_SelectionChangeFcn(hObject,eventdata)
colour = getappdata(handles.figure1,'Colour');
hold on
for i = 1:noFiles
plot(f(2:plotLength),dataStore(2:plotLength,i),'color',colour)
end
hold off
Before this I initialise 'Colour' to blue with:
setappdata(handles.figure1,'Colour','blue')
The figure plots so I know it is executing the line :
plotColour_SelectionChangeFcn(hObject,eventdata)
But It doesn't seem to be updating the 'Colour' value. Its almost as if none of the tags in the switch/case loop are matching, but I don't know why. I have check and doubled checked the tags for the radio buttons in the property inspector.
Any help would be really appreciated, Thanks in advance :)
Tim

Réponse acceptée

Sean de Wolski
Sean de Wolski le 20 Juil 2012
First, the keyword is the American spelling of 'color' :)
Second, you don't want to set the figure's color but rather the handle to the plots' color (whether it be a line patch or whatever.
function example_buttongroup
figure;
axes('pos',[.3 .3 .5 .5]);
hL = plot(1:10);
huib = uibuttongroup('selectionchangefcn',{@schange, hL},'pos',[.1 .1 .2 .2]);
uicontrol('style','radio','string','b','parent',huib,'units','norm','pos',[.1 .5 .4 .4]);
uicontrol('style','radio','string','r','parent',huib,'units','norm','pos',[.1 .1 .4 .4]);
function schange(src,evt,hL)
set(hL,'color',get(evt.NewValue,'string'));
  9 commentaires
Tim Mottram
Tim Mottram le 23 Juil 2012
I don't think so. Is this what I need to do? Is the selectionchangefcn special in some way and needs that extra line of code? if so where do I put that line and can you explain what you mean by ObjH being the handle of the called object? Thanks for helping, I'm really tearing my hair out here! :)
Tim Mottram
Tim Mottram le 23 Juil 2012
Hi, I have managed to sort it. Totally my fault for copying code, I did take it of the Matlab website but I think that was for people that didn't use GUIDE.. The problem was this line. :
function plotColour_SelectionChangeFcn(hObject,eventdata)
needed an extra ",handles" as an input argument. But thanks for tipping me off, it was you asking if I had passed the handles structure that made me wonder. Thanks alto for your help guys, Ill try and be more careful in future. :)

Connectez-vous pour commenter.

Plus de réponses (1)

Jan
Jan le 23 Juil 2012
Modifié(e) : Jan le 23 Juil 2012
If the variable handles is used, it has to be defined anywhere in the current workspace. This is not special for the SelectionChangeFcn, but a basic fact for all functions. These details are explained in the "Getting Started" chapters of the documentation.
If your function is defined as "plotColour_SelectionChangeFcn(hObject,eventdata)", you can get the figure's handles struct by:
handles = guidata(hObject);
such that my "hObj" is "hObject" here. You find more details at "doc guidata".

Catégories

En savoir plus sur Migrate GUIDE Apps 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