简体   繁体   English

从C调用MATLAB

[英]Calling MATLAB from C

I'm writing a Java application that needs to be able to run MATLAB commands. 我正在编写一个需要能够运行MATLAB命令的Java应用程序。 To do so, I'm using a C program that the Java application can call upon to interface with MATLAB. 为此,我正在使用Java应用程序可以调用的C程序来与MATLAB接口。 However, even after researching the MATLAB engine, I can't seem to figure out how to compile the C program. 但是,即使在研究了MATLAB引擎之后,我似乎也无法弄清楚如何编译C程序。 This documentation seems to be compiling the C program from within MATLAB: http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/f39903.html . 该文档似乎是从MATLAB内部编译C程序的: http : //www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/f39903.html Is there any way to compile from the command line? 有什么办法可以从命令行进行编译吗? That is, can I use gcc with some flags to include all the relevant MATLAB support (I don't ordinarily program in C, so sorry if my language is not exactly correct!) 也就是说,我可以使用带有某些标志的gcc来包括所有相关的MATLAB支持(我通常不使用C进行编程,如果我的语言不完全正确,请抱歉!)

Thanks! 谢谢!

You can compile your code normally as you would do for a normal C program, but you need to specify the dependencies correctly. 您可以像对普通C程序那样正常地编译代码,但是需要正确指定依赖项。

The best way to do is to use the CMake utility with a CMakelists.txt file. 最好的方法是将CMake实用程序与CMakelists.txt文件一起使用。 Now, a demo on how to write the CMakeLists.txt is given here and how to interface C/C++ with Matlab is given here . 现在,关于如何写的CMakeLists.txt演示给出这里以及如何连接C / C ++用Matlab给出了这里 I hope it helps.... 希望对您有所帮助。

I don't use MATLAB, but I'm guessing that you have to do something like this: 我不使用MATLAB,但我猜测您必须执行以下操作:

  1. Compile your C program with MATLAB libraries to create a shared library that Java can use. 使用MATLAB库编译C程序,以创建Java可以使用的共享库。
  2. Write a JNI interface that calls your shared library, being sure to link in your new SO and all those from MATLAB that you need. 编写一个调用共享库的JNI接口,确保链接到新的SO和您需要的来自MATLAB的所有SO。

Break the problem into steps and you'll sort it out. 将问题分解为几个步骤,您将对其进行解决。

Maybe you want do the following : 也许您想执行以下操作:

1) Compile your matlab code (ie m files ) with the matlab compiler mcc from the matlab command line. 1)使用matlab编译器mcc从matlab命令行编译您的matlab代码(即m文件)。 mcc compiler generates c dll . mcc编译器生成c dll。 The matlab generated c dll contains the c interface for the matlab m files. Matlab生成的c dll包含matlab m文件的c接口。 See the following link about how to generate c dll from matlab M files. 请参阅以下链接,了解如何从matlab M文件生成c dll。 C Shared Library Target C共享库目标

2) Write c dll that uses the generated matlab dll in step 1. Compile the c dll with c compiler.See the following link about how to call the functions that are inside the matlab generated dll MATLAB Compiler Generated Interface Functions 2)在步骤1中编写使用生成的matlab dll的c dll。使用c编译器编译c dll。有关如何调用matlab生成的dll内部函数的信息,请参见以下链接。MATLAB Compiler生成的接口函数

3) Use the generated c dll in step 2 with java. 3)将步骤2中生成的c dll与Java配合使用。

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

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