[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: enum in octave?
From: |
Søren Hauberg |
Subject: |
Re: enum in octave? |
Date: |
Mon, 13 Oct 2008 08:47:05 +0200 |
fre, 10 10 2008 kl. 11:49 -0700, skrev Jim H:
> Hi,
> I think the question I want to ask is "does Octave have the concept of enum
> as in C?"
>
> My application: I would like to pass a set of arguments to a function in an
> array (p[ ]), but I want to refer to the elements of the array by mnemonic
> names like:
>
> y[BOB] = p[R]*y[BOB]*(1 - y[BOB]/p[K]);
>
> in C this could be done with #define R 0 #define K 1, etc. or by an enum
> structure.
>
> Possible in octave?
I don't think Octave has something similar to enum, but you can easily
code something up. The following function
function [varargout] = enum (first_index = 1)
for k = 1:nargout
varargout {k} = k + first_index - 1;
endfor
endfunction
should allow you to do something like
[ALICE, BOB, CARL] = enum ()
which will produce
ALICE = 1
BOB = 2
CARL = 3
And, if you want the first index to be different from 1, you can do
[ALICE, BOB, CARL] = enum (2)
you'll get
ALICE = 2
BOB = 3
CARL = 4
Hope that helps,
Søren
>
> Jim