From: Maarten van Reeuwijk on
Dear group,

For post-processing purposes, I am using eval to ungroup data in structures. This works beautifully, except when the structure contains variables which are also function names. My specific problem is with the variable beta (the thermal expansion coefficient), which clashes with the built-in beta function. If I do it without using eval, it evaluates properly:

a.beta = 2;
beta = a.beta;
printvar(beta * 20);

But if I do exactly the same with eval, it doesn't!

a.beta = 2;
eval('beta=a.beta;');
printvar(beta * 20);

Apparently, the variable is not properly registered as a local variable when I use eval. Using whos does show that beta is properly defined. Does any of you have an idea how can I solve this? Not using eval or renaming beta are no options.

Many thanks,

Maarten

PS. Full test problem below

% -------- code fragment -------
function test
test1 % works
test2 % works
test3 % does not work

function test1
beta = 2;
disp ('----------')
printvar(beta * 20);

function test2
a.beta = 2;
beta = a.beta;
printvar(beta * 20);

function test3
a.beta = 2;
eval('beta=a.beta;');
printvar(beta * 20);

function printvar(var)
num2str(var)
From: us on
"Maarten van Reeuwijk" <m.van.reeuwijk(a)erase.gmx.net> wrote in message <i0i6ak$2fh$1(a)fred.mathworks.com>...
> Dear group,
>
> For post-processing purposes, I am using eval to ungroup data in structures. This works beautifully, except when the structure contains variables which are also function names. My specific problem is with the variable beta (the thermal expansion coefficient), which clashes with the built-in beta function. If I do it without using eval, it evaluates properly:

well... that makes it very simple: do NOT use EVAL...
case closed...

us
btw, your example works fine here (r2010a)...
From: Maarten van Reeuwijk on
> > If I do it without using eval, it evaluates properly:
>
> well... that makes it very simple: do NOT use EVAL...
> case closed...

Very funny. However, I pre-empted comments like this further down the post:

>> Not using eval or renaming beta are no options.

> btw, your example works fine here (r2010a)...

That is curious. I am running MATLAB 7.10.0.499 (R2010a) on a 64 bit linux machine. It also does not work with a R2009b version on a 32 bit Windows machine.

Maarten
From: us on
"Maarten van Reeuwijk" <m.van.reeuwijk(a)erase.gmx.net> wrote in message <i0ia0p$79j$1(a)fred.mathworks.com>...
> > > If I do it without using eval, it evaluates properly:
> >
> > well... that makes it very simple: do NOT use EVAL...
> > case closed...
>
> Very funny. However, I pre-empted comments like this further down the post:
>
> >> Not using eval or renaming beta are no options.
>
> > btw, your example works fine here (r2010a)...
>
> That is curious. I am running MATLAB 7.10.0.499 (R2010a) on a 64 bit linux machine. It also does not work with a R2009b version on a 32 bit Windows machine.
>
> Maarten

well... then let's look at

% replace your simple PRINTVAR with its simple one-line command NUM2STR
% and make sure
which num2str;
% points to the correct location...

again, the example works fine - why should it not...
us
From: Steven Lord on
"Maarten van Reeuwijk" <m.van.reeuwijk(a)erase.gmx.net> wrote in message
news:i0i6ak$2fh$1(a)fred.mathworks.com...
> Dear group,
>
> For post-processing purposes, I am using eval to ungroup data in
> structures. This works beautifully, except when the structure contains
> variables which are also function names. My specific problem is with the
> variable beta (the thermal expansion coefficient), which clashes with the
> built-in beta function. If I do it without using eval, it evaluates
> properly:
>
> a.beta = 2;
> beta = a.beta;
> printvar(beta * 20);
>
> But if I do exactly the same with eval, it doesn't!
>
> a.beta = 2;
> eval('beta=a.beta;');
> printvar(beta * 20);
>
> Apparently, the variable is not properly registered as a local variable
> when I use eval. Using whos does show that beta is properly defined. Does
> any of you have an idea how can I solve this? Not using eval or renaming
> beta are no options.

I strongly, STRONGLY encourage you to reconsider your position that "Not
using eval or renaming beta are no options". Search this newsgroup for the
term "poofing" (think of a magician making a rabbit appear in a puff of
smoke) to understand why this is a VERY BAD IDEA.

If you MUST (and by MUST I mean your boss says "You're fired if you don't do
this") use EVAL to poof variables into the workspace, you will need to give
MATLAB a clear indication that the identifier will be a variable that it can
see _when it parses the function_. [The string that you pass into the EVAL
function does NOT give MATLAB that clear indication.] To do this, have the
identifier appear on the left side of an equals sign:

a.beta = 2;
beta = 0;
eval('beta = a.beta;')

But note that if you're going to do that, you could avoid the EVAL with
either dynamic field names:

a.beta = 2;
fieldToExtract = 'beta';
printvar(a.(fieldToExtract)*20)

or simply performing the assignment sans EVAL:

a.beta = 2;
beta = a.beta;

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com