how to return vector of strings?

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

how to return vector of strings?

Tom Goles

Hi,
I'm trying to return a vector of character strings from dynamically
loadable c++ function (.oct file).
For example, if my function is called 'Test' then from octave I'd like
to call it as follows:

octave> a = Test()
a =

Hello
world

octave>


In other words, the vector that gets returned should be the same as if
I simply typed:

octave> a = ["Hello";"world"]
a =

Hello
world

octave>

My problem is that I don't know which octave c++ class i'm supposed to
use to achieve this result. I have string values stored in regular 2-d
c char array and need to copy them to some octave class which will then
be returned to octave.
Any help/examples would be appreciated.
Thanks,
Tomislav Goles

ps. BTW, I have no trouble returning vectors of real numbers from dynamically
loaded functions - for that I used ColumnVector class (as shown in the
oregonator.cc example).

Reply | Threaded
Open this post in threaded view
|

how to return vector of strings?

John W. Eaton-6
On 22-Apr-1997, Tom Goles <[hidden email]> wrote:

| I'm trying to return a vector of character strings from dynamically
| loadable c++ function (.oct file).
| For example, if my function is called 'Test' then from octave I'd like
| to call it as follows:
|
| octave> a = Test()
| a =
|
| Hello
| world
|
| octave>
|
| In other words, the vector that gets returned should be the same as if
| I simply typed:
|
| octave> a = ["Hello";"world"]
| a =
|
| Hello
| world

Use Octave's string_vector class (header file is str-vec.h,
distributed in the liboctave directory).

  #include <octave/oct.h>

  DEFUN_DLD (test, , ,
    "usage: test ()\n\
  \n\
  Return a friendly array of strings.")
  {
    string_vector retval (11);

    retval(0) = "hello";
    retval(1) = "world";
    retval(2) = "these";
    retval(3) = "are";
    retval(4) = "some";
    retval(5) = "strings";
    retval(6) = "in";
    retval(7) = "a";
    retval(8) = "friendly";
    retval(9) = "string";
    retval(10) = "vector";

    return retval;
  }

jwe