From: Jan Simon on 18 Feb 2010 13:34 Dear Jens! > I still have the problem that when I press the radiobutton Beer the listbox shows the 4 strings with beer, but when I press the pushbutton plot it do not plot the right function. 'M3_Beer_Heineken' is number 3 in the cellarray stringcell, but it is plotted as choice 2. Hope you understand. Sorry I am soo bad to explain by problem. As I have written before, you cannot use the value of the listbox. Get the name of the list box as string and find the corresponding index from the list of names. Kind regards, Jan
From: Cokelid on 18 Feb 2010 14:26 On Feb 18, 1:34 pm, "Jan Simon" <matlab.THIS_Y...(a)nMINUSsimon.de> wrote: > As I have written before, you cannot use the value of the listbox. Get the name of the list box as string and find the corresponding index from the list of names. Jan is right of course. The index you get from the listbox refers to what is shown in the listbox at that time, not to the stringcell values that used to be in the listbox that you've since overwritten. But really the code is all wrong. I'm sorry to say but I think you need to start again from scratch. As an aside, your radio buttons should be part of a uibuttongroup so they toggle correctly. Regards, Justin
From: jens on 19 Feb 2010 03:51 Cokelid <cokelid(a)googlemail.com> wrote in message <d54ac4a8-a0f0-4675-b214-242c176317ae(a)j6g2000vbd.googlegroups.com>... > On Feb 18, 1:34 pm, "Jan Simon" <matlab.THIS_Y...(a)nMINUSsimon.de> > wrote: > > > As I have written before, you cannot use the value of the listbox. Get the name of the list box as string and find the corresponding index from the list of names. > > Jan is right of course. The index you get from the listbox refers to > what is shown in the listbox at that time, not to the stringcell > values that used to be in the listbox that you've since overwritten. > But really the code is all wrong. I'm sorry to say but I think you > need to start again from scratch. > > As an aside, your radio buttons should be part of a uibuttongroup so > they toggle correctly. > > Regards, > > Justin Dear Jan and Justin I have tried to rewrite my code an using a uibuttongroup instead of single radiobuttons. It make my code more compact. Jan: You surgested that I should use the get function to get the value of the listbox when I have pushed a radio button. The problem is that I only get one value by doing: Value = get(ListBox, 'value'); %Listbox=hlb. I need to get the listbox to detect that for Beer it should detect choice==2 and choice==3? I tried to get the corresponding index of the listbox strings by using a function I called smartseqsearch that takes two inputs the cellarray to search and the strings I want to find. smartseqsearch(stringcell,Book)=[2 5]; Now I have a vector with the index of the listbox strings. How do I now make sure that the listbox detect that for Beer it should detect choice==2 and choice==3? If I try something like choice=set(Listbox,'value',Value_1),where Value_1 is the index of the listbox strings it gives me error. Sorry if I ask many questions, but I do not give up on my problem there must be a way out:) I have copied my updated code under. Best Regards Jens h = uibuttongroup('visible','off','Position',[0.6 0.2 .1 0.6],'backgroundcolor',col); u0 = uicontrol('Style','Radio','String','Book',... 'pos',[10 400 100 30],'parent',h,'HandleVisibility','off','backgroundcolor',col); u1 = uicontrol('Style','Radio','String','Beer',... 'pos',[10 350 100 30],'parent',h,'HandleVisibility','off','backgroundcolor',col); set(h,'SelectionChangeFcn',@selcbk); set(h,'SelectedObject',[]); set(h,'Visible','on'); function selcbk(source,eventdata) get(h,'SelectionChangeFcn') get(source) get(eventdata.OldValue,'String') RadioButtonName=get(eventdata.NewValue,'String') stringcell={'M1_Beer_Carlsberg';'M2_Book_Football';'M3_Beer_Heineken';'M4_Beer_Newcastle';'M5_Book_Basketball';'M6_Beer_Slots'}; containsBeer = cellfun(@(c) any(strfind(c, RadioButtonName)), stringcell); noBeer = stringcell(containsBeer); Value_1=smartseqsearch(stringcell,noBeer) set(hlb, 'String', noBeer); end hlb=uicontrol('style','listbox',... 'units','normalized',... 'position',[0.10 0.6 0.15 0.1],... 'string',stringcell); function go1(varargin) choice=get(hlb,'value') if choice==1 figure(2) x=[1:10] plot(x,x.^2) elseif choice==2 figure(2) x=[1:10] plot(x,x.^3) elseif choice==3 figure(2) x=[1:10] plot(x,x.^4) elseif choice==4 figure(2) x=[1:10] plot(x,x.^5) elseif choice==5 figure(2) x=[1:10] plot(x,x.^6) elseif choice==6 figure(2) x=[1:10] plot(x,x.^7) end end uicontrol('style','pushbutton',... 'units','normalized',... 'position',[0.125 0.5 0.10 0.04],... 'string','Plot ',... 'backgroundcolor',col,... 'callback',@go1); end
From: jens on 19 Feb 2010 05:06 "jens " <storebonghoved(a)hotmail.com> wrote in message <hlljdo$fvp$1(a)fred.mathworks.com>... > Cokelid <cokelid(a)googlemail.com> wrote in message <d54ac4a8-a0f0-4675-b214-242c176317ae(a)j6g2000vbd.googlegroups.com>... > > On Feb 18, 1:34 pm, "Jan Simon" <matlab.THIS_Y...(a)nMINUSsimon.de> > > wrote: > > > > > As I have written before, you cannot use the value of the listbox. Get the name of the list box as string and find the corresponding index from the list of names. > > > > Jan is right of course. The index you get from the listbox refers to > > what is shown in the listbox at that time, not to the stringcell > > values that used to be in the listbox that you've since overwritten. > > But really the code is all wrong. I'm sorry to say but I think you > > need to start again from scratch. > > > > As an aside, your radio buttons should be part of a uibuttongroup so > > they toggle correctly. > > > > Regards, > > > > Justin > > Dear Jan and Justin > > I have tried to rewrite my code an using a uibuttongroup instead of single radiobuttons. It make my code more compact. > > Jan: > You surgested that I should use the get function to get the value of the listbox when I have pushed a radio button. The problem is that I only get one value by doing: > Value = get(ListBox, 'value'); %Listbox=hlb. > I need to get the listbox to detect that for Beer it should detect choice==2 and choice==3? > I tried to get the corresponding index of the listbox strings by using a function I called smartseqsearch that takes two inputs the cellarray to search and the strings I want to find. > smartseqsearch(stringcell,Book)=[2 5]; > > Now I have a vector with the index of the listbox strings. How do I now make sure that the listbox detect that for Beer it should detect choice==2 and choice==3? > If I try something like choice=set(Listbox,'value',Value_1),where Value_1 is the index of the listbox strings it gives me error. > Sorry if I ask many questions, but I do not give up on my problem there must be a way out:) > I have copied my updated code under. > > > Best Regards > > Jens > > h = uibuttongroup('visible','off','Position',[0.6 0.2 .1 0.6],'backgroundcolor',col); > u0 = uicontrol('Style','Radio','String','Book',... > 'pos',[10 400 100 30],'parent',h,'HandleVisibility','off','backgroundcolor',col); > u1 = uicontrol('Style','Radio','String','Beer',... > 'pos',[10 350 100 30],'parent',h,'HandleVisibility','off','backgroundcolor',col); > set(h,'SelectionChangeFcn',@selcbk); > set(h,'SelectedObject',[]); > set(h,'Visible','on'); > > function selcbk(source,eventdata) > get(h,'SelectionChangeFcn') > get(source) > get(eventdata.OldValue,'String') > RadioButtonName=get(eventdata.NewValue,'String') > stringcell={'M1_Beer_Carlsberg';'M2_Book_Football';'M3_Beer_Heineken';'M4_Beer_Newcastle';'M5_Book_Basketball';'M6_Beer_Slots'}; > containsBeer = cellfun(@(c) any(strfind(c, RadioButtonName)), stringcell); > noBeer = stringcell(containsBeer); > Value_1=smartseqsearch(stringcell,noBeer) > set(hlb, 'String', noBeer); > end > > > hlb=uicontrol('style','listbox',... > 'units','normalized',... > 'position',[0.10 0.6 0.15 0.1],... > 'string',stringcell); > > function go1(varargin) > > choice=get(hlb,'value') > > if choice==1 > figure(2) > x=[1:10] > plot(x,x.^2) > elseif choice==2 > figure(2) > x=[1:10] > plot(x,x.^3) > elseif choice==3 > figure(2) > x=[1:10] > plot(x,x.^4) > elseif choice==4 > figure(2) > x=[1:10] > plot(x,x.^5) > elseif choice==5 > figure(2) > x=[1:10] > plot(x,x.^6) > elseif choice==6 > figure(2) > x=[1:10] > plot(x,x.^7) > end > end > > uicontrol('style','pushbutton',... > 'units','normalized',... > 'position',[0.125 0.5 0.10 0.04],... > 'string','Plot ',... > 'backgroundcolor',col,... > 'callback',@go1); > > end Dear Jan and Justin I found a solution on my problem!!!Thanks to both of you. Now it works, it is probably not the best way I did it. First I found the indexes of the updated strings in the listbox by using the smartseqsearch function that has two inputs the cellarray to search through and a cellarray with the strings I wants. The indexes is passed to the listbox by defining the indexes as a global variable.Next I definds a if statement in the listbox: function go1(varargin) Index fchoice=get(hlb,'value') if fchoice==1 choice=Index(1) elseif fchoice==2 choice=Index(2) elseif fchoice==3 choice=Index(3) elseif fchoice==4 choice=Index(4) end fchoice is the values from the listbox that are wrong when I updates the strings. Index are the indexes of the updated strings. There is probably a much smarter way to do it, but now it works:) Instead of defind the Index as a global variabel I can probably pass it to the go1 function in another way. Best Regards Jens Just for your interest my whole code is: function Test() close all global Index Index = []; % Initialize f=figure('name','Testing Data',... 'numbertitle','off',... 'menubar','none',... 'color',[0.85, 0.85, 0.85],... 'units','normalized',... 'position',[0 0.1 0.95 0.87]); col=get(f,'color'); stringcell={'M1_Beer_Carlsberg';'M2_Book_Football';'M3_Beer_Heineken';'M4_Beer_Newcastle';'M5_Book_Basketball';'M6_Beer_Slots'}; h = uibuttongroup('visible','off','Position',[0.6 0.2 .1 0.6],'backgroundcolor',col); u0 = uicontrol('Style','Radio','String','Book',... 'pos',[10 400 100 30],'parent',h,'HandleVisibility','off','backgroundcolor',col); u1 = uicontrol('Style','Radio','String','Beer',... 'pos',[10 350 100 30],'parent',h,'HandleVisibility','off','backgroundcolor',col); set(h,'SelectionChangeFcn',@selcbk); set(h,'SelectedObject',[]); set(h,'Visible','on'); function selcbk(source,eventdata) get(h,'SelectionChangeFcn') get(source) get(eventdata.OldValue,'String') RadioButtonName=get(eventdata.NewValue,'String') stringcell={'M1_Beer_Carlsberg';'M2_Book_Football';'M3_Beer_Heineken';'M4_Beer_Newcastle';'M5_Book_Basketball';'M6_Beer_Slots'}; containsBeer = cellfun(@(c) any(strfind(c, RadioButtonName)), stringcell); noBeer = stringcell(containsBeer); Index=smartseqsearch(stringcell,noBeer) set(hlb, 'String', noBeer); end hlb=uicontrol('style','listbox',... 'units','normalized',... 'position',[0.10 0.6 0.15 0.1],... 'string',stringcell); function go1(varargin) Index fchoice=get(hlb,'value') if fchoice==1 choice=Index(1) elseif fchoice==2 choice=Index(2) elseif fchoice==3 choice=Index(3) elseif fchoice==4 choice=Index(4) end if choice==1 figure(2) x=[1:10] plot(x,x.^2) elseif choice==2 figure(2) x=[1:10] plot(x,x.^3) elseif choice==3 figure(2) x=[1:10] plot(x,x.^4) elseif choice==4 figure(2) x=[1:10] plot(x,x.^5) elseif choice==5 figure(2) x=[1:10] plot(x,x.^6) elseif choice==6 figure(2) x=[1:10] plot(x,x.^7) end end uicontrol('style','pushbutton',... 'units','normalized',... 'position',[0.125 0.5 0.10 0.04],... 'string','Plot ',... 'backgroundcolor',col,... 'callback',@go1); end
First
|
Prev
|
Pages: 1 2 3 Prev: mex file define a sparse matrix Next: detect mouse input, not in matlab instance |