简体   繁体   中英

Octave C++ API (feval)- how to pass multiple parameters to feval?

Environment : Windows 7 Professional + octave 3.6.2 + Visual C++

I was trying to embed octave into a standalone C++ program according to the tutorial:

http://www.gnu.org/software/octave/doc/interpreter/Standalone-Programs.html#Standalone-Programs

I managed to run the first program without problem, but the second one gives error message.

Simplified version of the second program :

int main (void)
{
    string_vector argv (2);
    argv(0) = "embedded";
    argv(1) = "-q";
    octave_main (2, argv.c_str_vec(), 1);

    Matrix a_matrix = Matrix (1, 2);
    std::cout << "GCD of [12, 16] is ";


    a_matrix(0)=12;
    a_matrix(1)=16;

    octave_value_list in = octave_value (a_matrix);
    octave_value_list out = feval ("gcd", in, 1);

    std::cout<<out(0).matrix_value()<<std::endl;
    return 0;

}

the line with "feval" failed to execute. The reason is that in octave 3.6.2 , the function gcd no longer accept list of value as parameter , one has to call gcd(value1, value2, ...) instead of gcd([value1, value2, ...]), which was supported back in octave 3.2.4 , so this bring up my main problem here:

How can I pass multiple parameters to feval as separate values, so that I can call functions like gcd(value1, value2, ...) through octave's C++ API?

Ultimately, I need to do some graphics processing in a GUI application, so I may need to call functions like conv2 at the C++ side (which sadly, also requires multiple function parameters)

Thank you in advance for any help

Well, I just made the substitution bellow:

//octave_value_list in = octave_value (a_matrix);
octave_value_list in;
for (octave_idx_type i = 0; i < n; i++)
   in(i) = a_matrix (i);

it works... but I get a jre error.

It turns out that directly passing octave_value_list as input, instead of converting Matrix to octave_value_list with octave_value , works fine. (Perhaps octave_value is the culprit?)

So the working code under octave 3.6.2 is like this:

int main (void)
{
    string_vector argv (2);
    argv(0) = "embedded";
    argv(1) = "-q";
    octave_main (2, argv.c_str_vec(), 1);

    std::cout << "GCD of [12, 16] is ";

    // Use octave_value_list directly as input
    octave_value_list in(2);    
    in(0)=12;
    in(1)=16;

    octave_value_list out = feval ("gcd", in, 1);

    std::cout<<out(0).int_value()<<std::endl;
    return 0;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM