help-octave
[Top][All Lists]
Advanced

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

C++ code calling octave and the FAQ


From: John W. Eaton
Subject: C++ code calling octave and the FAQ
Date: Fri, 04 Apr 2008 18:20:17 -0400

On  5-Apr-2008, Worik wrote:

| I was hoping that I could get some help with this, so I am asking again.
| 
| FAQ says...
| 
| #include <octave/oct.h>
|   ...
|   ColumnVector NumRands(2);
|   NumRands(0) = 9000;
|   NumRands(1) = 1;
|   octave_value_list f_arg, f_ret;
|   f_arg(0) = octave_value(NumRands);
|   f_ret = feval("rand",f_arg,1);
|   Matrix unis(f_ret(0).matrix_value());
| 
| I competed this snippet into a programme thusly...
| 
| #include <octave/oct.h>
| #include <octave/parse.h>
| #include <iostream>
| int main(){
|   ColumnVector NumRands(2);
|   NumRands(0) = 9000;
|   NumRands(1) = 1;
|   octave_value_list f_arg, f_ret;
|   f_arg(0) = octave_value(NumRands);
|   f_ret = feval("rand",f_arg,1);
|   Matrix unis(f_ret(0).matrix_value());
|   std::cout<<__FILE__<<":"<<__LINE__<<":"<<unis;
|   return 0;
| }

In order for feval to work, you have to initialize the interpreter.
For example, you need something like this:

  #include <octave/oct.h>
  #include <octave/octave.h>
  #include <octave/parse.h>
  #include <iostream>

  int
  main (int argc, char **argv)
  {
    if (octave_main (argc, argv, 1))
      {
        ColumnVector NumRands (2);
        NumRands(0) = 9000;
        NumRands(1) = 1;

        octave_value_list f_arg;
        f_arg(0) = octave_value (NumRands);

        octave_value_list f_ret = feval ("rand", f_arg, 1);

        if (f_ret.length () > 0)
          {
            Matrix unis (f_ret(0).matrix_value ());

            std::cout << __FILE__ << ":" << __LINE__ << ":" << unis;
          }
        else
          error ("feval failed");
      }
    else
      error ("Octave interpreter initialization failed");

    return error_state ? 1 : 0;
  }

| Compiled it on my ubuntu box thus:
| g++ -g3 -o test -O0 test.cc -L /usr/lib/octave-2.9.12 -loctave
| -loctinterp 

You can also use

  mkoctfile --link-stand-alone test.cc

jwe


reply via email to

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