help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


From: Svante Signell
Subject: Re: [Fwd: How to make a read variable av lvalue?]
Date: Wed, 23 Jan 2008 23:30:45 +0100

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 <address@hidden>
> > To: address@hidden
> > Cc: address@hidden
> > 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 =
  {
  }

}



reply via email to

[Prev in Thread] Current Thread [Next in Thread]