[Fwd: How to make a read variable av lvalue?]

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

[Fwd: How to make a read variable av lvalue?]

Svante Signell
This was sent to the wrong list. Hoping for some answers here.

-------- Forwarded Message --------
From: Svante Signell <[hidden email]>
To: [hidden email]
Cc: [hidden email]
Subject: How to make a read variable av lvalue?
Date: Wed, 23 Jan 2008 08:19:21 +0100

Dear list,

I have the following problem when coding in Octave/Matlab. After reading
a string from an external file into a variable, like x=fscanf(...)
resulting in x='string' how can I use this variable content as an
lvalue, like x.a='something', where x is replaced by its string value,
resulting in string.a='something' instead of x.a='something'.

For functions calls eval() can be used but what about string
substitution? BTW: We are writing som general software in Matlab/Octave
and are planning to use eval() extensively in loops. How slow it is
compared to fixed function calls?

Thanks
_______________________________________________
Help-octave mailing list
[hidden email]
https://www.cae.wisc.edu/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: [Fwd: How to make a read variable av lvalue?]

Muthiah Annamalai-3
Svante Signell wrote:

> This was sent to the wrong list. Hoping for some answers here.
>
> -------- Forwarded Message --------
> From: Svante Signell <[hidden email]>
> To: [hidden email]
> Cc: [hidden email]
> Subject: How to make a read variable av lvalue?
> Date: Wed, 23 Jan 2008 08:19:21 +0100
>
> Dear list,
>
> I have the following problem when coding in Octave/Matlab. After reading
> a string from an external file into a variable, like x=fscanf(...)
> resulting in x='string' how can I use this variable content as an
> lvalue, like x.a='something', where x is replaced by its string value,
> resulting in string.a='something' instead of x.a='something'.
>
> For functions calls eval() can be used but what about string
> substitution? BTW: We are writing som general software in Matlab/Octave
> and are planning to use eval() extensively in loops. How slow it is
> compared to fixed function calls?
>
> Thanks
>  

I think using eval() is not a good idea in general, specifically if you
want to eval()
input from the user, it could dangerously compromise security.

Maybe you should rework your algorithm?

There are ways to "install-global-variables" into Octave, and use them.
You could
use a .oct file, and create the variable on the Octave symbol-table.

I suggest thinking of another way to do whatever you want to do right
now. Loops
aren't performance friendly either. Octave suggests users to vectorize
the code.

-Muthu

_______________________________________________
Help-octave mailing list
[hidden email]
https://www.cae.wisc.edu/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: [Fwd: How to make a read variable av lvalue?]

Søren Hauberg
In reply to this post by Svante Signell

ons, 23 01 2008 kl. 09:18 +0100, skrev Svante Signell:
> I have the following problem when coding in Octave/Matlab. After reading
> a string from an external file into a variable, like x=fscanf(...)
> resulting in x='string' how can I use this variable content as an
> lvalue, like x.a='something', where x is replaced by its string value,
> resulting in string.a='something' instead of x.a='something'.
I'm not sure if I've understood your problem correctly, but is this what
you are looking for:

octave:4> eval(sprintf('%s.a = "something"', 'x'))
x =
{
  a = something
}

? There's possibly a faster or more elegant way of doing this, but I
can't think of anything right now.


> For functions calls eval() can be used but what about string
> substitution? BTW: We are writing som general software in Matlab/Octave
> and are planning to use eval() extensively in loops. How slow it is
> compared to fixed function calls?
I don't know. But I guess it's easy to figure out.

octave:12> Xs = linspace(1,100,1000000);
octave:13> tic, for x = Xs, sin(x); endfor, toc
Elapsed time is 7.26 seconds.
octave:14> tic, for x = Xs, feval('sin',x); endfor, toc
Elapsed time is 15.1 seconds.

So, when comparing 'sin(x)' to 'feval("sin", x)' there seems to be a
factor 2 speed loss. But I guess this depends on the function. I would
assume that for more complicated functions written as m-files, the speed
loss would be minimal.

Søren


_______________________________________________
Help-octave mailing list
[hidden email]
https://www.cae.wisc.edu/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: [Fwd: How to make a read variable av lvalue?]

Thomas Weber-8
In reply to this post by Svante Signell

Am Mittwoch, den 23.01.2008, 09:18 +0100 schrieb Svante Signell:

> This was sent to the wrong list. Hoping for some answers here.
>
> -------- Forwarded Message --------
> From: Svante Signell <[hidden email]>
> To: [hidden email]
> Cc: [hidden email]
> Subject: How to make a read variable av lvalue?
> Date: Wed, 23 Jan 2008 08:19:21 +0100
>
> Dear list,
>
> I have the following problem when coding in Octave/Matlab. After reading
> a string from an external file into a variable, like x=fscanf(...)
> resulting in x='string' how can I use this variable content as an
> lvalue, like x.a='something', where x is replaced by its string value,
> resulting in string.a='something' instead of x.a='something'.

octave:1> x = "horray"
x = horray
octave:2> eval ([x, ".a = 300"]);
horray =
{
  a =  300
}

octave:3> horray.a
ans =  300


> For functions calls eval() can be used but what about string
> substitution? BTW: We are writing som general software in Matlab/Octave
> and are planning to use eval() extensively in loops. How slow it is
> compared to fixed function calls?

You are combining two slow features,  so expect it to be slow.

        Thomas

_______________________________________________
Help-octave mailing list
[hidden email]
https://www.cae.wisc.edu/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: [Fwd: How to make a read variable av lvalue?]

Svante Signell
In reply to this post by Muthiah Annamalai-3
Thanks for your replies, the code works now, see below. However, using
eval() creates not so readable code, and accessing variables whose
string value is the lvalue (the variable name of interest) is
cumbersome. Maybe there are better ways to do this. Comments are
welcome.

On Tue, 2008-01-22 at 23:57 -0600, Muthiah Annamalai wrote:

> Svante Signell wrote:
> > This was sent to the wrong list. Hoping for some answers here.
> >
> > -------- Forwarded Message --------
> > From: Svante Signell <[hidden email]>
> > To: [hidden email]
> > Cc: [hidden email]
> > Subject: How to make a read variable av lvalue?
> > Date: Wed, 23 Jan 2008 08:19:21 +0100
> >
> > Dear list,
> >
> > I have the following problem when coding in Octave/Matlab. After reading
> > a string from an external file into a variable, like x=fscanf(...)
> > resulting in x='string' how can I use this variable content as an
> > lvalue, like x.a='something', where x is replaced by its string value,
> > resulting in string.a='something' instead of x.a='something'.
> >
> > For functions calls eval() can be used but what about string
> > substitution? BTW: We are writing som general software in Matlab/Octave
> > and are planning to use eval() extensively in loops. How slow it is
> > compared to fixed function calls?
> >
> > Thanks
> >  
>
> I think using eval() is not a good idea in general, specifically if you
> want to eval()
> input from the user, it could dangerously compromise security.
>
> Maybe you should rework your algorithm?
>
> There are ways to "install-global-variables" into Octave, and use them.
> You could
> use a .oct file, and create the variable on the Octave symbol-table.
>
> I suggest thinking of another way to do whatever you want to do right
> now. Loops
> aren't performance friendly either. Octave suggests users to vectorize
> the code.
>
> -Muthu
>

Conditons: The function read_param(), not given here returns the value
TEST in the string variable test_eval_s read from file test_eval.ini
To test this code, replace the calls to read_param() by fixed values:

test_eval_s.APP = 'TEST'; and str2='TEST';

NOTE: The calls to read_param (file_name,parameter_name) below are
commented out.

%Common code

% Normally this variable name is read into the program by e.g. a
read_param statement or parsing the call argument list: octave
test_value.m!!

Pname = 'test_eval';

delimiter = '/';
top = '.';

%Hard-coded lvalues:
fprintf('... Hardcoded ...\n')
% Hardcoded
test_eval_s = struct;
%test_eval_s.APP = read_param ([top,Pname,'.ini'],'APP')
test_eval_s.APP = 'TEST';

% APP path: APPS/TEST
Ipath = ['.',delimiter,'APPS',delimiter,test_eval_s.APP,delimiter]

% Override file: APPS/TEST/test_templates.ini
Ofile = [Ipath,Pname,'_',test_eval_s.APP,'.ini']

test_eval_s.MODULE_s = struct

%Using eval:
fprintf('... Using eval() ...\n')
eval (sprintf ('%s = struct;', [Pname,'_s']))

str1 = [Pname,'_s.APP'];
%str2 = read_param([top,Pname,'.ini'], 'APP');
str2 = 'TEST';

eval (sprintf ('%s = str2;', str1))
eval (str1)

% APP path: APPS/TEST
Ipath = ['.',delimiter,'APPS',delimiter,eval(str1),delimiter]

% Override file: APPS/TEST/test_templates.ini
Ofile = [Ipath,Pname,'_',eval(str1),'.ini']

eval (sprintf ('%s.MODULE_s = struct;', [Pname,'_s']))
eval([Pname,'_s'])

Expected (primary) output:

Ipath = ./APPS/TEST/
Ofile = ./APPS/TEST/test_eval_TEST.ini
test_eval_s =
{
  APP = TEST
  MODULE_s =
  {
  }

}

_______________________________________________
Help-octave mailing list
[hidden email]
https://www.cae.wisc.edu/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: [Fwd: How to make a read variable av lvalue?]

Przemek Klosowski
In reply to this post by Muthiah Annamalai-3
   Svante Signell wrote:
   >
   > I have the following problem when coding in Octave/Matlab. After reading
   > a string from an external file into a variable, like x=fscanf(...)
   > resulting in x='string' how can I use this variable content as an
   > lvalue, like x.a='something', where x is replaced by its string value,
   > resulting in string.a='something' instead of x.a='something'.

As others suggested, you have picked the slow and intricate solution,
which is probably an overkill, so I would suggest to reassess whether
you really need the eval(). I am guessing that you want a flexible
namespace for your variables, but the same goal would be accomplished
if you used a hash array, like in Perl or Tcl:

 $x="foobar"                                   set x foobar
 $myVariables{$x} = 'something'                set myVariables($x) 1

Given the first-class hash arrays, you can implement all the other
compound structures: $myVariables{$x}{field}, etc.

Octave doesn't have a built-in hash array type, but it comes pretty
close with the variant structure construct: similarly to the examples
above, you can have a structure with fields determined by a variable:

       x='abc';
       myVariables.(x)='something'

The added advantage of this approach relative to what you proposed
is that it creates a sort of a namespace: all related variables are
collected under the common 'root' name ('myVariables' in the example
above); you can pass them all together as a variable to subroutines,
make them global, etc.
_______________________________________________
Help-octave mailing list
[hidden email]
https://www.cae.wisc.edu/mailman/listinfo/help-octave