From: jens on
"Jan Simon" <matlab.THIS_YEAR(a)nMINUSsimon.de> wrote in message <hpo8nl$5q$1(a)fred.mathworks.com>...
> Dear Matt!
>
> > What on Earth could you need so many cases for?
>
> E.g. pseudo hardware random number generator. You gauge it with a real fair unbiased dice. Then:
>
> function X = RND
> persistent n
> if isempty(n)
> n = uint32(1);
> else
> n = n + 1;
> end
> switch n
> case 1, X = 4;
> case 2, X = 2;
> case 3, X = 6;
> case 4, X = 3;
> etc... % I think you got it now
> otherwise
> X = 1;
> n = 0;
> end
> return;
>
> Much better than the old-fashioned modulo and bitshift stuff. I admit, it took a while until 2^32-1 cases have been programmed. The program would run faster, if we had the "n++" operator in Matlab.
>
> Jan

Thank you very much everybody. I have listbox in a GUI where i use a switch statement to pick the different choices of strings in the listbox.


function[]=test(varargin)


lineHdl1=ones(1,length(stringcell))*-1; %stringcell contain all the strings in the listbox
lineTag1=get(hlb,'string');

hlb=findobj('tag','bong'); %tag property of the listbox

choice=get(hlb,'value');

switch choice

case 1
x=1:10;
y=3*x.^2;
lineHdl1(1)=plot(x,y,'color',[rand rand rand]);
hold on
case 2
x=1:10;
y=2*x.^2;
lineHdl1(2)=plot(x,y,'color',[rand rand rand]);
hold on
.......
.......
end
validHdl1 = find(ishandle(lineHdl1))
legend(lineHdl1(validHdl1),lineTag1(validHdl1));

end

I have 100 cases. I use a switch statements instead of a if statement when there are that many cases. My code work fine, but ofcourse I am interested to hear if there is a way to optimise it that why I asked if there is a limit on how many cases a switch statement can contain.

Another question: Is it possible to tag/pick more than one string in a listbox at the same time? The reason why I ask is that sometimes it can be time consuming to tag every string you want to plot in a listbox and then press the pushbutton everytime.

Best Regards

Jens
From: Walter Roberson on
jens wrote:

> Thank you very much everybody. I have listbox in a GUI where i use a
> switch statement to pick the different choices of strings in the listbox.
>
>
> function[]=test(varargin)
>
>
> lineHdl1=ones(1,length(stringcell))*-1; %stringcell contain all the
> strings in the listbox
> lineTag1=get(hlb,'string');
>
> hlb=findobj('tag','bong'); %tag property of the listbox
>
> choice=get(hlb,'value');
>
> switch choice

Create a function for each of the possible choices, put the handles to
the functions into a cell array. When the user selects, index the handle
array by the choice and invoke the function so selected.

> Another question: Is it possible to tag/pick more than one string
> in a listbox at the same time?

Set its Maximum property to more than 1. Be warned, though, that as soon
as the user single-clicks, all the previously chosen selections
disappear: you have to shift-click and control-click to add additional
elements. In my experience, it is _likely_ that if the user is selecting
more than a couple of items, that they will slip up along the line and
accidentally single-click when they meant to control-click, so in my
callback code, I check whether there were already multiple items
selected and the new selection would be a single item; in that case, I
infer that the user wanted to add the item, not replace the whole list.