[Top][All Lists]

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

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

From: Søren Hauberg
Subject: Re: [Fwd: How to make a read variable av lvalue?]
Date: Wed, 23 Jan 2008 09:36:35 +0100

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.


reply via email to

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