Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Octave c++ and VS2010

I'm trying to Use Octave with Visual C++.

I have downloaded octave-3.6.1-vs2010-setup-1.exe. Created a new project, added octave include folder to include path, octinterp.lib and octave.lib to lib path, and I added Octave bin folder as running directory.

The program compiles and runs fine except feval function that causes the exception:

Microsoft C++ exception: octave_execution_exception at memory location 0x0012faef

and on Octave side:

Invalid resizing operation or ambiguous assignment to an out-of-bounds array element.

What am I doing wrong?


Code for a standalone program:

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

int main(int argc, char **argv)
{
    if (octave_main (argc, argv, true))
    {
        ColumnVector NumRands(2);
        NumRands(0) = 10;
        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());
    }
    else
    {
        error ("Octave interpreter initialization failed");
    }
    return 0;
}

Thanks in advance.

like image 770
user629926 Avatar asked Jun 19 '12 11:06

user629926


2 Answers

I tried it myself, and the problem seems to originate from the feval line.

Now I don't have an explanation as to why, but the problem was solved by simply switching to the "Release" configuration instead of the "Debug" configuration.

I am using the Octave3.6.1_vs2010 build, with VS2010 on WinXP.

Here is the code I tested:

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

int main(int argc, char **argv)
{
    // Init Octave interpreter
    if (!octave_main(argc, argv, true)) {
        error("Octave interpreter initialization failed");
    }

    // x = rand(10,1)
    ColumnVector sz(2);
    sz(0) = 10; sz(1) = 1;
    octave_value_list in = octave_value(sz);
    octave_value_list out = feval("rand", in, 1);

    // print random numbers
    if (!error_state && out.length () > 0) {
        Matrix x( out(0).matrix_value() );
        std::cout << "x = \n" << x << std::endl;
    }

    return 0;
}

with an output:

x =
 0.165897
 0.0239711
 0.957456
 0.830028
 0.859441
 0.513797
 0.870601
 0.0643697
 0.0605021
 0.153486
like image 190
Amro Avatar answered Oct 24 '22 14:10

Amro


I'd guess that its actually stopped pointing at the next line and the error actually lies at this line:

f_arg(0) = octave_value(NumRands);

You seem to be attempting to get a value (which value?) from a vector and then assigning it to element 0 of a vector that has not been defined as a vector.

I don't really know though ... I've never tried writing octave code like that. I'm just trying to work it out by translating the code to standard matlab/octave code and that line seems really odd to me ...

like image 23
Goz Avatar answered Oct 24 '22 15:10

Goz