我正在尝试将以下 Matlab/Octave 程序转换为 C++/liboctave。
% s is struct with 3 real vector fields and 2 scalars.
s = load("L_octave.mat");
Lpos = sparse(s.i, s.j, s.v, s.m, s.n);
[~,dpos] = eigs(Lpos, 51, 'sm');
disp(diag(dpos));
我尝试遵循
https://docs.octave.org/latest/Standalone-Programs.html: 中的
standalonebuiltin.cc
示例
#include <iostream>
#include <octave/oct.h>
#include <octave/octave.h>
#include <octave/parse.h>
#include <octave/interpreter.h>
#include <octave/builtin-defun-decls.h>
int main()
{
octave::interpreter interpreter;
interpreter.execute();
octave_value_list in;
in(0) = "L_octave.mat";
octave_value_list out = Fload(interpreter, in);
// if uncommented fails with terminate called after throwing an instance 'octave::execution_exception' what(): octave_base_value::map_value(): wrong type argument '<unknown type>'
// octave_scalar_map arg0 = out(0).scalar_map_value();
// octave_value i = arg0.contents("i");
return 0;
}
这个示例编译并运行,但到目前为止我无法理解如何访问结构字段(或调整
.mat
读取代码)并构建稀疏矩阵。
关于 Octave 的 C++ 接口 / liboctave 的文档似乎有点稀疏(我尝试过 https://docs.octave.org/latest/Structures-in-Oct_002dFiles.html,但无法弄清楚如何访问结构体的字段)
感谢您的帮助!
函数
Fload
具有以下签名:
extern OCTINTERP_API octave_value_list
Fload (octave::interpreter&,
const octave_value_list& = octave_value_list (),
int = 0);
最后一个参数是函数输出参数的数量,默认为
0
。这里您需要一个输出参数,因此您应该将其设置为 1
:
octave_value_list out = Fload(interpreter, in, 1);
将输出数量设置为
0
与在Octave中调用load函数而不请求输出相同:
load L_octave.mat
在这种情况下,.mat 文件中的所有变量都会分配给解释器当前工作区中的相应变量。