MATLAB输出与Linux中的C/ c++程序的集成

时间:2022-08-31 17:28:40

I have designed a neural network in MATLAB. I saved the 'net' file which including the structure of the trained network and other required parameters.

我在MATLAB中设计了一个神经网络。我保存了“net”文件,包括经过训练的网络的结构和其他必需的参数。

Also, I have a program written by C language in Linux (Ubuntu) which generate some data in each second. I want to integrate my 'net' from MATLAB with these generated data. I mean, the 'net' gives each generated data from written program, then print the output based on its training.

另外,我有一个用Linux (Ubuntu)编写的程序,每秒钟生成一些数据。我想把MATLAB中的“net”与这些生成的数据集成在一起。我的意思是,“net”提供从编写的程序中生成的每个数据,然后根据它的训练打印输出。

How can i integrate a MATLAB program with a written program by C language in Linux?

如何将MATLAB程序与Linux中的C语言编写的程序集成在一起?

2 个解决方案

#1


2  

You can use Matlab engine for this task. Basically what I understand is that you need to send data from C/C++ to matlab, run some matlab codes and get a result back to c++.

您可以使用Matlab引擎完成此任务。基本上我所理解的是,你需要从C/ c++到matlab发送数据,运行一些matlab代码,然后返回到c++。

I have some C++ code for this task.

对于这个任务,我有一些c++代码。

The Code could be downloaded from https://github.com/thusithaC/CppToMat

代码可以从https://github.com/thesac/cpptomat下载

Basically using this you can,

用这个你可以,

1)Open a matlab engine from within a native progam 2) Call whatever matlab functions you want 3) Transfer data to/from matlab workspace into a native program.

1)在本机progam中打开matlab引擎2)调用您想要的任何matlab函数3)将数据从matlab工作区传输到本机程序中。

Mind you that the interface might not be super fast. So if you have a fast native program which runs a lot of iterations, calling these matlab routines might slow things up, especially if large data transfers are there. But this utility is mighty useful in debugging stuff, especially if you are first prototyping in matlab and then moving to CPP.

请注意,接口可能不会很快。如果你有一个快速的本机程序,它会运行很多次迭代,调用这些matlab例程可能会使事情变慢,尤其是当存在大量数据传输时。但是这个实用程序在调试方面非常有用,特别是如果您是在matlab中进行的第一个原型,然后转移到CPP。

#2


1  

If you want your C program to run as part of the same execution as Matlb, you need to use Mex. http://www.mathworks.com.au/help/matlab/matlab_external/introducing-mex-files.html

如果您希望您的C程序作为与Matlb相同的执行的一部分运行,则需要使用Mex。http://www.mathworks.com.au/help/matlab/matlab_external/introducing-mex-files.html

Mex allows your C program to loop just like any other function in Matlab. The "mex" command will compile your program into a shared object that is dynamically linked with Matlab. You need to write a special wrapper aound your C function to unpack arguments from Matlab and pack up the results. Debugging Mex code can be tricky so make sure your C functions are well debugged first.

Mex允许你的C程序在Matlab中像其他函数一样循环。“mex”命令将把您的程序编译成一个与Matlab动态链接的共享对象。您需要编写一个特殊的包装器来包装您的C函数,以便从Matlab中解压缩参数并打包结果。调试Mex代码可能比较麻烦,所以要确保您的C函数首先被调试好。

#1


2  

You can use Matlab engine for this task. Basically what I understand is that you need to send data from C/C++ to matlab, run some matlab codes and get a result back to c++.

您可以使用Matlab引擎完成此任务。基本上我所理解的是,你需要从C/ c++到matlab发送数据,运行一些matlab代码,然后返回到c++。

I have some C++ code for this task.

对于这个任务,我有一些c++代码。

The Code could be downloaded from https://github.com/thusithaC/CppToMat

代码可以从https://github.com/thesac/cpptomat下载

Basically using this you can,

用这个你可以,

1)Open a matlab engine from within a native progam 2) Call whatever matlab functions you want 3) Transfer data to/from matlab workspace into a native program.

1)在本机progam中打开matlab引擎2)调用您想要的任何matlab函数3)将数据从matlab工作区传输到本机程序中。

Mind you that the interface might not be super fast. So if you have a fast native program which runs a lot of iterations, calling these matlab routines might slow things up, especially if large data transfers are there. But this utility is mighty useful in debugging stuff, especially if you are first prototyping in matlab and then moving to CPP.

请注意,接口可能不会很快。如果你有一个快速的本机程序,它会运行很多次迭代,调用这些matlab例程可能会使事情变慢,尤其是当存在大量数据传输时。但是这个实用程序在调试方面非常有用,特别是如果您是在matlab中进行的第一个原型,然后转移到CPP。

#2


1  

If you want your C program to run as part of the same execution as Matlb, you need to use Mex. http://www.mathworks.com.au/help/matlab/matlab_external/introducing-mex-files.html

如果您希望您的C程序作为与Matlb相同的执行的一部分运行,则需要使用Mex。http://www.mathworks.com.au/help/matlab/matlab_external/introducing-mex-files.html

Mex allows your C program to loop just like any other function in Matlab. The "mex" command will compile your program into a shared object that is dynamically linked with Matlab. You need to write a special wrapper aound your C function to unpack arguments from Matlab and pack up the results. Debugging Mex code can be tricky so make sure your C functions are well debugged first.

Mex允许你的C程序在Matlab中像其他函数一样循环。“mex”命令将把您的程序编译成一个与Matlab动态链接的共享对象。您需要编写一个特殊的包装器来包装您的C函数,以便从Matlab中解压缩参数并打包结果。调试Mex代码可能比较麻烦,所以要确保您的C函数首先被调试好。