从 C++ api 访问八度结构成员
Access octave struct member from c++ api
我必须创建一个 c++ 程序来使用 Octave C++ API 读取和操作图像。图像为 .mat 二进制格式。
要以八度音阶读取文件,我使用
S = load("/path/to/file", "-mat", "house") // house is the var name
和S
是标量结构类型。
为了用 C++ 读取文件,我创建了以下函数
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();
}
}
利用了Octave的load内置加载功能。它读取文件并将结果保存到 out
变量,但我无法访问结构的 house
变量成员。我也尝试将 out(0)
转换为 octave_scalar_struct
但仍然没有成功。所以我的问题是如何从 Octave C++ API?
访问 out(0)
结构的变量
编辑:
注意:struct的变量应该是double
的数组
您可以使用subsref
提取变量“house”:
octave_value val = out(0).subsref(".", {ovl("house")});
Matrix mat = val.matrix_value ();
假设house
是一个矩阵。您可以将其作为双精度值的 N 维数组获取:
NDArray arr = val.array_value ();
其他数据类型也可用。
我必须创建一个 c++ 程序来使用 Octave C++ API 读取和操作图像。图像为 .mat 二进制格式。 要以八度音阶读取文件,我使用
S = load("/path/to/file", "-mat", "house") // house is the var name
和S
是标量结构类型。
为了用 C++ 读取文件,我创建了以下函数
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();
}
}
利用了Octave的load内置加载功能。它读取文件并将结果保存到 out
变量,但我无法访问结构的 house
变量成员。我也尝试将 out(0)
转换为 octave_scalar_struct
但仍然没有成功。所以我的问题是如何从 Octave C++ API?
out(0)
结构的变量
编辑: 注意:struct的变量应该是double
的数组您可以使用subsref
提取变量“house”:
octave_value val = out(0).subsref(".", {ovl("house")});
Matrix mat = val.matrix_value ();
假设house
是一个矩阵。您可以将其作为双精度值的 N 维数组获取:
NDArray arr = val.array_value ();
其他数据类型也可用。