help-octave
[Top][All Lists]
Advanced

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

how to call an octave .m file from a C++ program


From: vermeij
Subject: how to call an octave .m file from a C++ program
Date: Wed, 1 Apr 2009 15:08:02 +0200
User-agent: Internet Messaging Program (IMP) H3 (4.1.3)

Hi,

I tried to find information on how to call an octave .m file from a C++ program. There is some information around, but most of it is no longer up-to-date, it was not trivial to get it working. In this post I want to explain how to do it, maybe it is useful to somebody.

I have a file called exampleOctaveFunction.m which looks like this:

-------------------------------------
function [resultScalar, resultString, resultMatrix] = exampleOctaveFunction (inScalar, inString, inMatrix)

  resultScalar = (inScalar * pi);
  resultString = strcat ('Good morning Mr. ', inString);
  resultMatrix = (inMatrix + 1);

endfunction
-------------------------------------

I have a file called how-to-call-octave.cpp which is this:

-------------------------------------
#include <octave/oct.h>
#include <octave/octave.h>
#include <octave/parse.h>
#include <octave/toplev.h> /* do_octave_atexit */

int main (const int argc, char ** argv)
{
const char * argvv [] = {"" /* name of program, not relevant */, "--silent"};

  octave_main (2, (char **) argvv, true /* embedded */);

  octave_value_list functionArguments;

  functionArguments (0) = 2;
  functionArguments (1) = "D. Humble";

  Matrix inMatrix (2, 3);

  inMatrix (0, 0) = 10;
  inMatrix (0, 1) = 9;
  inMatrix (0, 2) = 8;
  inMatrix (1, 0) = 7;
  inMatrix (1, 1) = 6;
  functionArguments (2) = inMatrix;

const octave_value_list result = feval ("exampleOctaveFunction", functionArguments, 1);

  std::cout << "resultScalar is " << result (0).scalar_value () << std::endl;
  std::cout << "resultString is " << result (1).string_value () << std::endl;
  std::cout << "resultMatrix is\n" << result (2).matrix_value ();

  do_octave_atexit ();
}
-------------------------------------

And a little readme file, called readme.sh, that explains how to compile and run this simple example:

-------------------------------------
#! /bin/bash

#
# Make sure you have octave installed. On my system, Ubuntu 8.10, I installed
# the packages octave3.0 and octave3.0-headers.
#
# To compile, type
#
make how-to-call-octave.o
#
# which, on my system, is equivalent to
#
#   g++ -c -o how-to-call-octave.o how-to-call-octave.cpp
#
# To link, type
#
g++ -L /usr/lib/octave-3.0.1/ -l octinterp -o how-to-call-octave how-to-call-octave.o
#
# Adjust the directory in the -L directive according to the configuration of
# your machine.
#
# To run, type
#
./how-to-call-octave
#
# It should output something like
#
#   resultScalar is 6.28319
#   resultString is Good morning Mr. D. Humble
#   resultMatrix is
#    11 10 9
#    8 7 1
#
# Hope this is of help to somebody.
#
-------------------------------------

You can also execute the readme.sh file and it will compile and run for you.

Good luck, Arjan.
**********************************************************************************************
*** PRIVILEGED AND CONFIDENTIAL ***
The information contained in this e-mail message (including any attached files)
is intended for the use of the addressee(s) only and is privileged information.
The information should neither be posted to the Internet, nor published in any
other public domain, without the express permission of the sender. If you are
not the intended recipient(s) or the recipient's representative, you are hereby
notified that any use, disclosure, copying or distribution of this communication
is prohibited. If you have received this communication in error please notify us
immediately at address@hidden and remove this message from your system.
**********************************************************************************************




reply via email to

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