为C程序创建MATLAB MEX文件 [英] Create a MATLAB MEX file for a C program

查看:120
本文介绍了为C程序创建MATLAB MEX文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一位经验丰富的MATLAB用户,但对C和MEX文件是全新的.我有一个用C编写的复杂程序,需要从MATLAB内调用.该程序由一个文件夹中的几十个文件组成,其中包括一个名为main.c的文件,该文件处理来自命令行的输入,并将结果传递给其他进行实际计算的类.

I'm an experienced MATLAB user but totally new to C and MEX files. I have a complex program written in C that I need to call from within MATLAB. The program consists of a few dozen files in a folder, including one called main.c that processes inputs from the command line passes the results to other classes that do the actual calculations.

通常,要从命令行安装此程序,我将在UNIX命令提示符下运行./configure.然后,要运行该程序,请执行./runMyProgram -f input_file.txt -p some_parameters.该程序将包含数字列表的文本文件作为输入,并在命令窗口中打印结果表.我想向程序提供一个MATLAB数组(而不是.txt文件),并获取一个数组(而不是打印的结果表).

Normally, to install this program from the command line, I would run ./configure, make at the UNIX command prompt. Then, to run the program, ./runMyProgram -f input_file.txt -p some_parameters. The program takes a text file consisting of a list of numbers as input and prints a table of results in the command window. I want to feed the program a MATLAB array (instead of a .txt file) and get back an array (instead of a printed table of results).

我已经阅读了The Mathworks的MEX文档(我发现它相当不透明)以及其他一些教程",但我完全迷失了-这些示例适用于非常简单的单文件C程序,而对于真正讨论如何处理更大,更复杂的程序.用执行相同操作的MEX文件替换main.c文件是否足够?另外,如何在MATLAB中编译整个程序包?

I have read the MEX documentation from The Mathworks (which I found rather opaque), as well as some other "tutorials", but I am totally lost - the examples are for very simple, single-file C programs and don't really discuss how to handle a larger and more complicated program. Is it enough to replace the main.c file with a MEX file that does the same things? Also, how do I compile the whole package within MATLAB?

我将对从何处开始的任何普通英语建议,或指向以可理解的方式处理该主题的任何教程的指针,表示感谢.

I would be grateful for any plain-English advice on where to start with this, or pointers to any tutorials that deal with the subject in a comprehensible way.

推荐答案

是.通常,用MEX文件替换main.c文件是该过程.在您的情况下,由于您已经具有复杂的构建设置,因此构建一个库然后构建一个单独的与该库链接的单独的mex文件可能会更容易.这比使用mex命令构建整个对象要容易得多.如果导出需要从库中调用的函数,则可以从mexFunction调用它. mexFunction可以完成mxArrays的所有创建和读取.一个简单的示例mexFunction可以是

Yes. Normally replacing a main.c file with MEX file is the process. In your case since you already have complex build setup, it might be easier to build a library and then build a separate mex file which just links against this library. This will be much easier than building the whole thing using mex command. If you export the function you need to call from your library, you can call it from your mexFunction. mexFunction can do all the creation and reading of mxArrays. A simple sample mexFunction can be,

#include "mex.h"
// Include headers for your library

void
mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
{
   void* x = mxGetData(prhs[0]); // Assume one input. Check nrhs
   plhs[0] = mxCreateDoubleMatrix(10,10,mxREAL); // Create 10x10 double matrix for output
   void* y = mxGetData(plhs[0]);
   yourLibraryFunction(x, y); // Read from x and write to y. Pass sizes in if needed
}

这篇关于为C程序创建MATLAB MEX文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆