Thread Subject: assign genvarname variable names without using eval?

Subject: assign genvarname variable names without using eval?

From: Johnathan

Date: 14 May, 2009 20:53:00

Message: 1 of 8

function genplotbuttons(obj,eventdata)
        m=get(jplotcolumns,'String')
        m=str2double(m)
        n=get(jplotrows,'String')
        n=str2double(n)
        names={'1','2','3','4','5','6','7'}

        xÁ
        y=r3
        w0
        h0
        index=1;
        for i=1:n
            for j=1:m
                passer=uicontrol('Style','popupmenu',...
                    'Position',[x,y,w,h],...
                    'String',names(index))
                varname=genvarname(['jsubplot','column',num2str(m),...
                    'row',num2str(n)]);
                eval([varname '= passer'])
                x=x+w;
               index=index+1;
            end
            xÁ;
            y=y-h;
        end
end

This function is nested, and when eval([varname '=passer'] tries to run, I get the now oh so familiar error message:

??? Attempt to add "jsubplotcolumn3row2" to a static workspace.
 See MATLAB Programming, Restrictions on Assigning to Variables for details.
Error in ==> plot_options_gui>genplotbuttons at 97
                eval([varname '= passer'])
Error in ==> plot_options_gui at 76
genplotbuttons

How can I assign the variable names as handles to the pushbuttons without using eval? There will be a user determined number of rows and columns of pushbuttons displayed on the gui, so I need to use genvarname to give them handles so I can refer to them later.

Thanks,
--John

Subject: assign genvarname variable names without using eval?

From: per isakson

Date: 14 May, 2009 22:58:01

Message: 2 of 8

"Johnathan " <durchfalldurchfall@yahoo.com> wrote in message <gui0bc$rgr$1@fred.mathworks.com>...
> function genplotbuttons(obj,eventdata)
> m=get(jplotcolumns,'String')
> m=str2double(m)
> n=get(jplotrows,'String')
> n=str2double(n)
> names={'1','2','3','4','5','6','7'}
>
> x?
> y=r3
> w0
> h0
> index=1;
> for i=1:n
> for j=1:m
> passer=uicontrol('Style','popupmenu',...
> 'Position',[x,y,w,h],...
> 'String',names(index))
> varname=genvarname(['jsubplot','column',num2str(m),...
> 'row',num2str(n)]);
> eval([varname '= passer'])
> x=x+w;
> index=index+1;
> end
> x?;
> y=y-h;
> end
> end
>
> This function is nested, and when eval([varname '=passer'] tries to run, I get the now oh so familiar error message:
>
> ??? Attempt to add "jsubplotcolumn3row2" to a static workspace.
> See MATLAB Programming, Restrictions on Assigning to Variables for details.
> Error in ==> plot_options_gui>genplotbuttons at 97
> eval([varname '= passer'])
> Error in ==> plot_options_gui at 76
> genplotbuttons
>
> How can I assign the variable names as handles to the pushbuttons without using eval? There will be a user determined number of rows and columns of pushbuttons displayed on the gui, so I need to use genvarname to give them handles so I can refer to them later.
>
> Thanks,
> --John

One possibility: use a struct and "Using Dynamic Field Names"
/per

Subject: assign genvarname variable names without using eval?

From: Johnathan

Date: 14 May, 2009 23:52:01

Message: 3 of 8

Per, thanks. That is exactly what I wanted to do, but didn't know what it was called. I found this video that was helpful.

http://blogs.mathworks.com/videos/2009/02/27/dynamic-field-name-usage/

I had tried something like this, but had no clue that the variable name had to be in parentheses, so obviously it didn't work. Seems easier than eval.

You can't just use these as variables, though, can you?
string='varname'
(string)=5 %an attempt to get a variable varname that equals 5

this gives an error. Is this only good for structures? it seems like it would be so nice to be able to arbitrarily turn a string into a variable. Can this be done? Any other cool things you can do with dynamic fields?

Do you know where info on this can be found in the help?

Thanks
--John

Subject: assign genvarname variable names without using eval?

From: per isakson

Date: 15 May, 2009 00:35:02

Message: 4 of 8

"Johnathan " <durchfalldurchfall@yahoo.com> wrote in message <guiar1$v$1@fred.mathworks.com>...
...
> Do you know where info on this can be found in the help?
>

>> sa = struct('Name', 'MyTest');
>> sa.('VarName') = 'wathsoever';
>> sa.('VarValue') = 17;
>> sa.VarValue^3
ans =
        4913
>> fprintf( 1, '%s = %f\n', sa.VarName, sa.VarValue )
wathsoever = 17.000000
>> fprintf( 1, '%s = %f\n', sa.('VarName'), sa.('VarValue') )
wathsoever = 17.000000
>> sa
sa =
        Name: 'MyTest'
     VarName: 'wathsoever'
    VarValue: 17
>>

You can replace a simple variable with a "field of a struct" in every(?) contexts.

Search for "Built-In Classes (Data Types)" and select "Structures" in the online help

/per

Subject: assign genvarname variable names without using eval?

From: Bruno Luong

Date: 15 May, 2009 06:30:04

Message: 5 of 8

"Johnathan " <durchfalldurchfall@yahoo.com> wrote in message <guiar1$v$1@fred.mathworks.com>...
> Per, thanks. That is exactly what I wanted to do, but didn't know what it was called. I found this video that was helpful.
>
> http://blogs.mathworks.com/videos/2009/02/27/dynamic-field-name-usage/
>
> I had tried something like this, but had no clue that the variable name had to be in parentheses, so obviously it didn't work. Seems easier than eval.
>
> You can't just use these as variables, though, can you?
> string='varname'
> (string)=5 %an attempt to get a variable varname that equals 5
>
> this gives an error. Is this only good for structures? it seems like it would be so nice to be able to arbitrarily turn a string into a variable. Can this be done? Any other cool things you can do with dynamic fields?
>

This allows you to do it:

http://www.mathworks.com/matlabcentral/fileexchange/23078

Bruno

Subject: assign genvarname variable names without using eval?

From: Steven Lord

Date: 15 May, 2009 14:02:13

Message: 6 of 8


"Johnathan " <durchfalldurchfall@yahoo.com> wrote in message
news:guiar1$v$1@fred.mathworks.com...
> Per, thanks. That is exactly what I wanted to do, but didn't know what it
> was called. I found this video that was helpful.
>
> http://blogs.mathworks.com/videos/2009/02/27/dynamic-field-name-usage/
>
> I had tried something like this, but had no clue that the variable name
> had to be in parentheses, so obviously it didn't work. Seems easier than
> eval.
>
> You can't just use these as variables, though, can you?
> string='varname'
> (string)=5 %an attempt to get a variable varname that equals 5

No, you can't.

> this gives an error. Is this only good for structures? it seems like it
> would be so nice to be able to arbitrarily turn a string into a variable.
> Can this be done?

Technically, yes. Should you do this? In my opinion, no. "Poofing"
variables into existence at runtime can cause problems, see Q4.6 in the
newsgroup FAQ:

http://matlabwiki.mathworks.com/MATLAB_FAQ

> Any other cool things you can do with dynamic fields?

I don't know ... that depends on what you consider "cool" :)

> Do you know where info on this can be found in the help?

In the most recent documentation on our website, here:

http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/br04bw6-38.html#br1v5a9-1

--
Steve Lord
slord@mathworks.com

Subject: assign genvarname variable names without using eval?

From: Matt

Date: 9 Jul, 2009 18:13:01

Message: 7 of 8

> > this gives an error. Is this only good for structures? it seems like it
> > would be so nice to be able to arbitrarily turn a string into a variable.
> > Can this be done?
>
> Technically, yes. Should you do this? In my opinion, no. "Poofing"
> variables into existence at runtime can cause problems, see Q4.6 in the
> newsgroup FAQ:
>
> http://matlabwiki.mathworks.com/MATLAB_FAQ


I've tried the first suggestion from Q4.6, but that wasn't working out very well for me.

What I'm trying to accomplish:
I have a large number of FITS files (each is [581,752]) from X data runs (each with the same number of files), and I would like to be able to make X vectors to hold the FITS files when reshaped (into a vector) without having to declare a variable for each data run (as i frequently analyze many data runs together).

The annoyance of making X variables is even present earlier in the program, when I have to read in the FITS file and then average each data run. Ideally it wouldn't involve concatenating these arrays into a 3d matrix, as that would kind of make it a pain to find the mean values for each pixel for a given data run (not to mention the memory issues this would create).

 This seems like there should be a simple way to do this, but I'm not sure how to use genvarname (even after reading and rereading the documentation) or if it's even a good idea.

Thanks in advance
matt

Subject: assign genvarname variable names without using eval?

From: Steven Lord

Date: 9 Jul, 2009 21:28:22

Message: 8 of 8


"Matt " <matteo.j.t@gmail.com> wrote in message
news:h35bvd$p6n$1@fred.mathworks.com...
>> > this gives an error. Is this only good for structures? it seems like it
>> > would be so nice to be able to arbitrarily turn a string into a
>> > variable.
>> > Can this be done?
>>
>> Technically, yes. Should you do this? In my opinion, no. "Poofing"
>> variables into existence at runtime can cause problems, see Q4.6 in the
>> newsgroup FAQ:
>>
>> http://matlabwiki.mathworks.com/MATLAB_FAQ
>
>
> I've tried the first suggestion from Q4.6, but that wasn't working out
> very well for me.
>
> What I'm trying to accomplish:
> I have a large number of FITS files (each is [581,752]) from X data runs
> (each with the same number of files), and I would like to be able to make
> X vectors to hold the FITS files when reshaped (into a vector) without
> having to declare a variable for each data run (as i frequently analyze
> many data runs together).

If all the files are the same size, then preallocate a matrix to hold the
data and fill it in rather than creating multiple variables.

XM = zeros((581*752), X);
for k = 1:X
    data = readInFile(k);
    XM(:, k) = data(:);
end

for an appropriate readInFile function (see Q4.12 in the FAQ linked above
for some suggestions as to functions you could use as the readInFile
function.)

Now wherever you would have referred to the variable containing image m, use
XM(:, m).

> The annoyance of making X variables is even present earlier in the
> program, when I have to read in the FITS file and then average each data
> run. Ideally it wouldn't involve concatenating these arrays into a 3d
> matrix, as that would kind of make it a pain to find the mean values for
> each pixel for a given data run

If I understand what you're describing, it's not that much of a pain:

Z = rand(2, 3, 4); % 4 data runs on 2-by-3 "images"
meanPixelValue = mean(mean(Z, 2), 1)

Compare with:

mean(reshape(Z(:, :, 1), 1, 6) % The mean of the first run

> (not to mention the memory issues this would create).

If you're reading in all your files at once, no matter how you store them
(one big matrix or individual smaller matrices) you could run into memory
issues.

> This seems like there should be a simple way to do this, but I'm not sure
> how to use genvarname (even after reading and rereading the documentation)
> or if it's even a good idea.

"Poofing" variables into existence at runtime is not recommended, as
mentioned in the FAQ entry mentioned above.

--
Steve Lord
slord@mathworks.com

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
functions Johnathan 14 May, 2009 19:54:04
nested Johnathan 14 May, 2009 19:54:04
dynamic Johnathan 14 May, 2009 19:54:04
fields Johnathan 14 May, 2009 19:54:04
eval Johnathan 14 May, 2009 17:04:27
genvarname Johnathan 14 May, 2009 17:04:27
rssFeed for this Thread

Contact us at files@mathworks.com