简体   繁体   English

使用数据接口将 C++ 数据存储到八度音阶?

[英]Store C++ data to octave using a data interface?

Matlab offers a data interface for C/C++, see here . Matlab 为 C/C++ 提供数据接口,请参见 此处 This allows users to export data from C/C++ into at runtime into Matlab formats (*.mat files).这允许用户在运行时将数据从 C/C++ 导出为 Matlab 格式(*.mat 文件)。 This means that you can export data from a C++ program and later return to the data using Matlab to analyze it.这意味着您可以从 C++ 程序中导出数据,然后使用 Matlab 返回数据进行分析。

I would like to start using an open-source alternative, Octave, in stead of Matlab.我想开始使用开源替代品 Octave,而不是 Matlab。 However, I am confused by the C/C++ interfaces that octave offers.但是,我对 octave 提供的 C/C++ 接口感到困惑。 What I understand so far is that I can call external routines from Octave using their external code interface .到目前为止我所理解的是,我可以使用 Octave的外部代码接口调用外部例程。

But can I also do the reverse, and store data from my C++ program to octave during runtime?但是我也可以反过来,并在运行时将我的 C++ 程序中的数据存储到八度音阶吗? If yes, pointers to a minimal example would be highly appreciated.如果是,将高度赞赏指向最小示例的指针。

This is probably not an answer for you, but writing it as a comment was too long and dense, so I'm writing as an answer for convenience, plus hopefully while trying to clarify things it may provide you with an answer anyway.这可能不是您的答案,但是将其写为评论太长且太密集了,因此为了方便起见,我将其写为答案,并且希望在尝试澄清事情时无论如何它都可以为您提供答案。

Essentially, there is some confusion in your question.从本质上讲,您的问题有些混乱。 It's worth clarifying these points first.值得首先澄清这些观点。 If my comments here don't answer your question, perhaps you can edit your question to clarify what you want exactly based on my comments below.如果我在这里的评论没有回答您的问题,也许您可以根据我下面的评论编辑您的问题以明确您想要什么。

  1. Matlab offers two interfaces, one for C and one for C++. Matlab 提供两种接口,一种用于 C,一种用于 C++。 These are completely separate interfaces.这些是完全独立的接口。

  2. These are general interfaces, for writing a function in, say, C, and calling that function from matlab, or vice versa.这些是通用接口,用于在 C 中写入 function,并从 function 调用 function,反之亦然。 They are not for "exporting data".它们不适用于“导出数据”。 It may be possible to store data from within C code into a.mat by calling the matlab "save" function via the provided api, but I can't think of a good reason to do this via C rather than as part of a matlab script which calls a C-written function to process some data, and then calls 'save' from matlab to save the result. It may be possible to store data from within C code into a.mat by calling the matlab "save" function via the provided api, but I can't think of a good reason to do this via C rather than as part of a matlab脚本调用 C 编写的 function 来处理一些数据,然后从 matlab 调用“保存”来保存结果。

  3. Octave supports matlab's "C" interface (so called ".mex" files). Octave 支持 matlab 的“C”接口(所谓的“.mex”文件)。 It does not support matlab's "C++" interface.它不支持 matlab 的“C++”接口。 But it does have its own "C++" interface, namely ".oct" files.但它确实有自己的“C++”接口,即“.oct”文件。

  4. If you have an example of a.mex file which performs what you ask, then presumably it can also be compiled in octave with little to no modification (except for including the right library names to reflect octave headers).如果您有一个执行您所要求的 a.mex 文件的示例,那么大概它也可以在几乎没有修改的情况下以八度编译(除了包含正确的库名称以反映八度头)。

  5. "I can call external routines from Octave" <-- you can assign general C++ code a function name and transform it, so that you can call it from octave. “我可以从 Octave 调用外部例程” <--您可以为通用 C++ 代码分配一个 function 名称并对其进行转换,以便您可以从 octave 调用它。 More to the point though, you can pass this function inputs from octave, let it handle the computations in C++, and then return a result back to octave.更重要的是,您可以从八度传递这个 function 输入,让它处理 C++ 中的计算,然后将结果返回到八度。 Is this what you mean by this?这就是你的意思吗? If so, then yes, this is possible, and the manual section you linked has examples of how to do this (btw, note that you are linking to an older version, the newest version of the manual is octave.org/doc/v5.2.0 )如果是这样,那么是的,这是可能的,并且您链接的手册部分有如何执行此操作的示例(顺便说一句,请注意您正在链接到旧版本,手册的最新版本是 octave.org/doc/v5 .2.0)

  6. Octave also provides an interface for calling Octave functions directly from C++ code (whether in the context of.oct files intended for use within octave, or in the context of standalone C++ executables which would like to use octave functions inside). Octave 还提供了一个接口,用于直接从 C++ 代码调用Octave函数(无论是在打算在 octave 内使用的 .oct 文件的上下文中,还是在想要在内部使用 octave 函数的独立 C++ 可执行文件的上下文中)。

When you say "store data", it seems to me that you are basically asking about point No.5, ie pass data as an input to a c++ written function, have c++ do something with that data, and then return its result back to the octave workspace.当您说“存储数据”时,在我看来,您基本上是在询问第 5 点,即将数据作为输入传递给 c++ 编写的 function,让 c++ 返回该数据并返回其结果八度工作区。 Is that correct?那是对的吗? Or are you literally asking for a way to create a.mat file from within c++ specifically?或者您在要求一种从 c++ 中创建 a.mat 文件的方法吗?

If what you're asking for is simply the former, then the manual provides many examples.如果您要求的只是前者,那么手册提供了许多示例。 Eg if you wanted to try the one near the end of this page: https://octave.org/doc/v5.2.0/Matrices-and-Arrays-in-Oct_002dFiles.html#Matrices-and-Arrays-in-Oct_002dFiles , then say that code is saved in file addtwomatrices.cpp , then you can compile (eg from your linux terminal) it like so:例如,如果您想尝试接近本页末尾的那个: https://octave.org/doc/v5.2.0/Matrices-and-Arrays-in-Oct_002dFiles.html#Matrices-and-Arrays-in-Oct_002dFiles ,然后说代码保存在文件addtwomatrices.cpp中,然后您可以像这样编译(例如,从您的 linux 终端):

$ mkoctfile -c addtwomatrices.cpp
$ mkoctfile ./addtwomatrices.o -o ./addtwomatrices

And then call it from octave like so:然后像这样从八度音程调用它:

> addtwomatrices( eye(2), magic(2) )
ans =
   5   3
   1   3

UPDATE更新

Here is an example of creating a matrix directly from C++ and then saving it to an octave-compatible format.这是直接从 C++ 创建矩阵然后将其保存为八度兼容格式的示例。

// in testmat.cpp
# include <octave/oct.h>
# include <octave/ls-oct-text.h>   /* defines save_text_data */

int main ()
{
 // Create a matrix and fill it with values
    Matrix M(3,3);
    for (int i = 0;  i < 9;  ++i )   { M.elem(i) = i+1.; }

 // Save to disk
    std::ofstream out_file_stream( "out.mat" );
    save_text_data ( out_file_stream,
                     octave_value(M),
                     std::string("M_matrix"),   // variable name
                     true,                      // mark as global?
                     5                          // precision
                   );

    out_file_stream.close();
}

Compile, link, and run:编译、链接和运行:

mkoctfile -c testmat.cpp
mkoctfile --link-stand-alone -o testmat testmat.o
LD_LIBRARY_PATH=/opt/octave-5.2.0/lib/octave/5.2.0 ./testmat   # change path accordingly

Confirm from octave out.mat was saved properly and can be opened in octave:从 octave 确认 out.mat 已正确保存并且可以在 octave 中打开:

octave:1> load out.mat
octave:2> M_matrix
M_matrix =
   1   4   7
   2   5   8
   3   6   9

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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