samedi 6 février 2021

Access octave struct member from c++ api

I have to create a c++ program to read and manipulate an image using the Octave C++ API. The image is in .mat binary format. To read the file in octave I use

S = load("/path/to/file", "-mat", "house") // house is the var name

and S is of type scalar structure.

To read the file in c++ I have a created the following function

namespace oc = octave;

void octave_read(char* file, std::vector<double>& v , const std::string& variable) {
    oc::interpreter inter {};
    try {
        inter.initialize_history (false);
        inter.initialize_load_path (false);
        inter.initialize();
        if (! inter.initialized ()){
            std::cerr << "Octave interpreter initialization failed!"
            << std::endl;
            exit (1);
        }
        octave_value_list in{};
        in(0) = file;
        in(1) ="-mat";
        in(2) = variable;
        octave_value_list out = Fload(inter, in, 1);
        // this should be replaced to get the actual values of the variable
        for (int i = 0; i < out(0).length(); ++i) {
            std::cout<< "got here" <<std::endl;
            std::cout<< out(0).class_name();
        }
    }  catch (const octave::exit_exception& ex){
        std::cerr << "Octave interpreter exited with status = "
                  << ex.exit_status () << std::endl;
    }
    catch (const octave::execution_exception& e)    {
        std::cerr << "error encountered in Octave evaluator!" << std::endl;
        std::cerr << e.info();
    }
}

which utilized the load builtin load function of Octave. It reads the file and saves the result to out variable but I cannot access the house variable-member of the struct. I also tried converting out(0) to octave_scalar_struct but still no luck. So my question is how can I access the variable of the out(0) struct from the Octave C++ API?

Aucun commentaire:

Enregistrer un commentaire