Why does button group not work?

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

 採用された回答

Sean de Wolski
Sean de Wolski 2012 年 7 月 20 日

0 投票

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 件のコメント

Tim Mottram
Tim Mottram 2012 年 7 月 20 日
Hi, thanks for your answer. I made sure not to use the English spelling anywhere where it would cause a problem, Its just a variable name.
I'm not really sure I understand where your coming from. Usually, when I plot some data I just use plot(x,y,'colour','myColour'). All I want to do is use the radio buttons to change the string held in myColour. I am calling the button function but for some reason its not recognising the radio button tags.. Also, wouldn't using the "figure" command open a new figure, outside my original GUI? I'm plotting to an axes on the GUI. I have made this using GUIDE if that changes anything.. Thanks for your help :)
Sean de Wolski
Sean de Wolski 2012 年 7 月 20 日
So what this is doing is replotting your line objects. This is bad ;) ! Why replot when you can just update? This is what I have above. I pass the handle to the line to my selectionchangefcn and it updates the color of the line.
Thus, if you:
hLine = plot(1:10,'color',colour)
setappdata(handles.figure1,'hLine',hLine)
You now have the handles to the line. Then in the selectionchangefcn:
set(getappdata(handles.figure1,'hLine'),'color',new_color);
The line will change color and you won't waste time/effort/memory replotting it (which could be significant for long lines)
And sorry, I did just realize you were setappdata-ing 'colour' and not setting it.
Tim Mottram
Tim Mottram 2012 年 7 月 20 日
I wasn't re-plotting. The program lets the user select as many files as they like, these are time series waveforms. The data is then passed through a series of algorithms and plotted. The user will then wish to view some more data (from a different test) on the same plot to analyse it. So they select a different radio button and when the new data is plotted the colour will be different. This isn't intended to just change the colour of the current plot data. In fact if the colour changes when the button is pushed this is bad. Sorry for not being clear before :)
Tim Mottram
Tim Mottram 2012 年 7 月 20 日
Seeing as I wasn't clear before Ill just tell you exactly what is happening. I have 100 files for each test. Each file contains 1024 data points which make up the time series waveform. This is passed through an fft routine. The spectra are now plotted on one graph (the tests had very little deviation so really its one thick line. Occasionally we get bad results, so there is a flycatcher algorithm which removes bad results. When the current dataset is free of errors the user selects loads the next set of data and performs the same process. The idea of the radio buttons is that before the new data is plotted a different colour is selected. So all I really want is the value in myColour to update, ready to be retrieved by the plot function. Only when I call the selectionchangefcn nothing happens, I might as well have just skipped over it...
Sean de Wolski
Sean de Wolski 2012 年 7 月 20 日
If you put a break point in the selectionchangefcn and hit a radio button does it get executed?
Tim Mottram
Tim Mottram 2012 年 7 月 23 日
Using breakpoints I can confirm that the selectionchangefcn is being executed. It makes me think that the value returned from:
get(hObject,'Tag')
Is not right as It isn't matching any of the case tags.
My selectionchangefcn seems to be behaving very oddly indeed. This line of code executes no problem when used in any other functioin:
bosh = 3;
setappdata(handles.figure1,'bosh',bosh)
But When using it anywhere in the selectionchangefcn gives this error:
Undefined variable "handles" or class "handles.figure1".
Error in ==> PlotGUI>plotColour_SelectionChangeFcn at 243 setappdata(handles.figure1,'bosh',bosh)
Error in ==> PlotGUI>plotData_Callback at 114 plotColour_SelectionChangeFcn(hObject,eventdata)
Error in ==> gui_mainfcn at 96 feval(varargin{:});
Error in ==> PlotGUI at 42 gui_mainfcn(gui_State, varargin{:});
Error in ==> @(hObject,eventdata)PlotGUI('plotData_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
Jan
Jan 2012 年 7 月 23 日
So did you obtain the handles struct in this subfunction?
handles = guidata(ObjH);
with ObjH is the handle of the called object?
Tim Mottram
Tim Mottram 2012 年 7 月 23 日
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 2012 年 7 月 23 日
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. :)

サインインしてコメントする。

その他の回答 (1 件)

Jan
Jan 2012 年 7 月 23 日
編集済み: Jan 2012 年 7 月 23 日

0 投票

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".

カテゴリ

ヘルプ センター および File ExchangeGraphics Performance についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by