Thread Subject: "eval" for GUI callback? storing generated strings in array?

Subject: "eval" for GUI callback? storing generated strings in array?

From: Rachel Laughs

Date: 13 Nov, 2009 00:05:19

Message: 1 of 7

I'm horrible at iterations and might be making this unnecessarily complicated, but here is my issue:


In my GUI, I have 54 pushbuttons that will have the exact same callback function. They are in a 9x6 arrangement, so their handles are designated by row-column element number. e.g.
"c11", "
c12", "c13", ... ,"c96"

It seems that the names would be easy enough to generate, convert to string, concatenate with string 'c' and voila, right....?


The expression that I would want the "eval" function to carry out is:
set(C11,'callback',{@cfunc,S})

but the pesky '' in the "callback"
gets in the way and it thinks that I am ending the string.

Suggestions??


OR:

Could I generate these 54 strings and put them into a 1x54 array?
(and just call upon the array in my "set" expression)


I made this to generate, but am not sure how to store into array:

for x = 1:9
    for y = 1:6
        x = num2str(x);
        y = num2str(y);
        rootname = 'c' ;
        fullname = strcat(rootname,strcat(x,y));
    end
end


But there's got to be a better and faster way....

Thanks in advance!

Subject: "eval" for GUI callback? storing generated strings in array?

From: Rachel Laughs

Date: 13 Nov, 2009 00:34:20

Message: 2 of 7

well obviously with 54 such short handles, i can just type them all out by hand, which took like 30 seconds, but I would still like to learn for creating things in the future...

thanks again!

Subject: "eval" for GUI callback? storing generated strings in array?

From: Matt Fig

Date: 13 Nov, 2009 00:58:03

Message: 3 of 7

how about using a cell array:

c{1},c{2},c{3},....c{54}

then later:

set(c{:},'callback',{@cfunc,S})


Otherwise,

str = ['set(c',num2str(2),',''callback'',','{@max,S})']
eval(str)

but I really recommend you do not use eval this way.

Subject: "eval" for GUI callback? storing generated strings in array?

From: Steven Lord

Date: 13 Nov, 2009 15:00:35

Message: 4 of 7


"Rachel Laughs" <remove.thisrachellaughs@gmail.com> wrote in message
news:hdi7rv$i14$1@fred.mathworks.com...
> I'm horrible at iterations and might be making this unnecessarily
> complicated, but here is my issue:
>
>
> In my GUI, I have 54 pushbuttons that will have the exact same callback
> function. They are in a 9x6 arrangement, so their handles are designated
> by row-column element number. e.g.
> "c11", "
> c12", "c13", ... ,"c96"
>
> It seems that the names would be easy enough to generate, convert to
> string, concatenate with string 'c' and voila, right....?
>
>
> The expression that I would want the "eval" function to carry out is:
> set(C11,'callback',{@cfunc,S})
>
> but the pesky '' in the "callback"
> gets in the way and it thinks that I am ending the string.
>
> Suggestions??

If the 54 pushbuttons have each been given their respective Tag property
value, their handles will be stored in the handles structure. You can use
dynamic field names to avoid the EVAL:

set(handles.('C11'), 'Callback', {@cfunc, S});

where you generate the string C** however you want.

Alternately, if you're creating these pushbuttons in a loop in your callback
function, you could create a matrix of handles C, store each handle into
that variable as you go, and set the Callback property on all the handles at
once.

set(C, 'Callback', {@cfunc, S});

Finally, another solution if you're creating the pushbuttons in a loop is to
simply set the Callback property in the call to UICONTROL in which you
create the button.

uicontrol('Style', 'pushbutton', 'Callback', {@cfunc, S}, ...

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: "eval" for GUI callback? storing generated strings in array?

From: Rachel Laughs

Date: 13 Nov, 2009 16:42:02

Message: 5 of 7

Thank you guys for all of your ideas. It's given me a lot of options to think about.


Steve, could you elaborate on a couple of points for me?

You mentioned:
> Alternately, if you're creating these pushbuttons in a loop in your callback
> function, you could create a matrix of handles C, store each handle into
> that variable as you go, and set the Callback property on all the handles at
> once.
>
> set(C, 'Callback', {@cfunc, S});
>
> Finally, another solution if you're creating the pushbuttons in a loop is to
> simply set the Callback property in the call to UICONTROL in which you
> create the button.
>
> uicontrol('Style', 'pushbutton', 'Callback', {@cfunc, S}, ...
>

That first idea was what I had originally been trying to figure out how to do. Since my pushbutton uicontrols were already all created (separately i might add), I was trying to come up with a loop to put the handles in a 1x54 matrix, but had difficulty. How would you set that up?


For that second idea.. how would you set it up to create all the pushbuttons in a loop? I wrote them out separately because they all have different positions and different background colors. I feel like I have an unnecessary amount of code, so a loop would be great!

Here is the layout I'd like:
- a 60px by 180px Figure
- In it, there are pushbuttons that are all 20px by 20px each, to cover the entire figure
- a total of 54 buttons: 9 rows, 6 columns
- each button has a different position: [x y 20 20]
if row 1, col. 1 = top left of figure, then as row increases, x decreases by 20; as column increases, y increases by 20
- each button has a completely different background color.

any ideas on how to create the loop to change the position property? and then would the background color just have to be changed by a "set" statement later?

Thanks again!

Subject: "eval" for GUI callback? storing generated strings in array?

From: Steven Lord

Date: 13 Nov, 2009 18:22:23

Message: 6 of 7


"Rachel Laughs" <remove.thisrachellaughs@gmail.com> wrote in message
news:hdk28q$3pp$1@fred.mathworks.com...
> Thank you guys for all of your ideas. It's given me a lot of options to
> think about.
>
>
> Steve, could you elaborate on a couple of points for me?
>
> You mentioned:
>> Alternately, if you're creating these pushbuttons in a loop in your
>> callback
>> function, you could create a matrix of handles C, store each handle into
>> that variable as you go, and set the Callback property on all the handles
>> at
>> once.
>>
>> set(C, 'Callback', {@cfunc, S});
>>
>> Finally, another solution if you're creating the pushbuttons in a loop is
>> to
>> simply set the Callback property in the call to UICONTROL in which you
>> create the button.
>>
>> uicontrol('Style', 'pushbutton', 'Callback', {@cfunc, S}, ...
>>
>
> That first idea was what I had originally been trying to figure out how to
> do. Since my pushbutton uicontrols were already all created (separately i
> might add), I was trying to come up with a loop to put the handles in a
> 1x54 matrix, but had difficulty. How would you set that up?

Now when you say "were already all created" did you mean you created them in
GUIDE, you created them programmatically in the initialization code, or
you're creating them programmatically in say a callback function (for
example, do you have a dropdown uicontrol where you let the user ask for a
certain grid of buttons?)

Anyway, I wouldn't have created a 1-by-54 matrix, I would have created a
9-by-6 matrix and stored each handle in the appropriate element of that
matrix as I was creating it.

nR = 9;
nC = 6;
buttonHandles = zeros(nR, nC);
for rows = 1:nR
    for cols = 1:nC
        buttonHandles(rows, cols) = uicontrol( ... );
    end
end

You can either set the Callback propery at creation or afterwards using the
handles stored in buttonHandles.

> For that second idea.. how would you set it up to create all the
> pushbuttons in a loop? I wrote them out separately because they all have
> different positions and different background colors. I feel like I have an
> unnecessary amount of code, so a loop would be great!
>
> Here is the layout I'd like:
> - a 60px by 180px Figure
> - In it, there are pushbuttons that are all 20px by 20px each, to cover
> the entire figure
> - a total of 54 buttons: 9 rows, 6 columns
> - each button has a different position: [x y 20 20]
> if row 1, col. 1 = top left of figure, then as row increases, x decreases
> by 20; as column increases, y increases by 20
> - each button has a completely different background color.
>
> any ideas on how to create the loop to change the position property? and
> then would the background color just have to be changed by a "set"
> statement later?

See above, using rows and cols to compute the appropriate Position property
vector. In your fourth bullet above, write x as a function of the height of
the figure and the value of cols, and y as a function of the width of the
figure and the value of rows. I did something similar to this in the BENCH
function several releases ago, to set up the layout of the results figures.
You could take a look at that for inspiration.

However, the way you've described the problem is inconsistent -- the way you
described it you don't want the buttons to overlap, and your figure is 60
pixels wide and 180 tall, but that means you can only fit a grid with 6 rows
of buttons and 3 columns in it. How did you decide you were going to be
able to fit 9 rows and 6 columns of buttons on it?

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: "eval" for GUI callback? storing generated strings in array?

From: Rachel Laughs

Date: 13 Nov, 2009 19:33:08

Message: 7 of 7

Ah, I see. I don't know why I had it stuck in my head that it had to be stored as some sort of row vector to be called upon. thank you for making it so simple! I haven't tried it out yet, but I believe I understand what you are saying.

As for the inconsistency, you are totally right. I had forgotten about that. That surprised me too. I was just playing around, trying to see if I could make my own color palette to change the color of another figure, so just made an arbitrary 60x180 px figure, with the intention of having a 3x9 grid of 20x20px buttons/colors, but when I ran it, it was obvious that the figure could be doubled horizontally, so i just added some more columns, and didn't even have to change the figure size property, and the buttons definitely look 20x20.

acutally. now that I look at it...I guess the figure window stretched on its own because the Windows XP bar thing at the top with the Logo-Min-Max-Close wouldn't all fit in a width of 60px. I guess 120px is the minimum width for new windows unless there is a way to get rid of that thing? I suppose I should change the width in my figure to be 120 for consistency instead of relying on windows!

oh and i'm doing everything programatically. I actually don't even know how to open up GUIDE hahaha

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
eval Rachel Laughs 12 Nov, 2009 19:09:03
callback Rachel Laughs 12 Nov, 2009 19:09:03
strings Rachel Laughs 12 Nov, 2009 19:09:03
rssFeed for this Thread

Contact us at files@mathworks.com