help-octave
[Top][All Lists]
Advanced

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

RE: dynamic list of variable names


From: Billings, Paul
Subject: RE: dynamic list of variable names
Date: Fri, 16 Sep 2005 09:14:25 -1000

I don't know about being "stuck" -- a structure is quite natural:
function ret = someother_function(Nvars)
  ...
  ret.variable_1 = 123;
  ret.variable_2 = 'abc';
  % etc.

You might also look at the setfield function, which uses a string (perhaps
generated via sprintf) to define the member names.

The tricky part of dynamic variables is *using* them in the calling
function -- you don't know what they are called!  In the case of structures,
however, you can find their names.  You can use "fieldnames" to get the list
of structure members.  The other approach of using a cell array will allow
easy access by numeric index.  (You can get at structure members
numerically, too, but it's not as direct as it is with cell arrays.)

Paul


> -----Original Message-----
> From: Colin Ingram [mailto:address@hidden
> Sent: Friday, September 16, 2005 7:59 AM
> To: address@hidden
> Subject: dynamic list of variable names
>
>
> How does one create a dynamic list of variables names at runtime.
>  So I'd like to do something like
>
> nr = some_function(x,y)
> for i = 1:nr
> ["some_variable_name" , "i"] = someother_function (i)
> end
>
> Which you obviously can't do since you can't have string
> concatenation on the left side of the assignment operator.
>
> It seams like I might be able to work something up using a
> variable list of return values from a function but I can't see
> how to do this.  Is it even possible or am I stuck using a
> structure or cell to hold my data?
>
>
>
> -------------------------------------------------------------
> Octave is freely available under the terms of the GNU GPL.
>
> Octave's home on the web:  http://www.octave.org
> How to fund new projects:  http://www.octave.org/funding.html
> Subscription information:  http://www.octave.org/archive.html
> -------------------------------------------------------------



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

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