我可以在网站上运行MATLAB代码吗? [英] Can I run MATLAB code on a web site?

查看:447
本文介绍了我可以在网站上运行MATLAB代码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个BE项目,其代码在MATLAB中,但是我需要在网页上显示结果.我想知道是否可以直接在网站上运行我的代码吗?如果没有,您能告诉我哪种语言是更好的选择?我在想ASP,HTML和PHP.

I have a BE project whose code is in MATLAB but I need to present results on a web page. I want to know whether I can run my code directly on a web site? If not, could you tell me which language would be a better option? I'm thinking maybe ASP, HTML and PHP.

推荐答案

您可以使用MATLAB编译器将MATLAB应用程序编译为独立的可执行文件.

You can compile your MATLAB application into a stand-alone executable using MATLAB compiler.

在提示符下键入"mcrversion",以确定是否已安装此软件包-如果您没有为此付费,则很可能没有.-与Mathworks提供的大多数扩展一样,您需要为此付费.

Type "mcrversion" at the prompt to determine whether you have this package installed- It's likely that you don't if you haven't paid for it- As with most extensions that Mathworks provides, you need to pay for it.

如果您不需要编译代码,而只需运行它,则可以通过命令行调用MATLAB以执行所需的操作.

If you don't need to compile your code, but simply run it, you may be able to invoke MATLAB through the command line to do what you need it to.

正如Sinan所述,在这两种情况下,您都将使用passthu之类的功能.

As Sinan mentioned, you would use a function like passthu in both of these cases.

另一种替代方法是为PHP创建扩展以在C语言中使用MATLAB.MATLAB提供了C API,使您可以使用MATLAB随附的库来调用引擎(有关示例,请参见"extern"文件夹).

Another alternative is to create an extension for PHP to utilize MATLAB in C. MATLAB provides a C API which allows you to call the engine using libraries that come with MATLAB (see your "extern" folder for examples).

有关创建扩展程序的信息,请参见以下链接(这很简单):

See the following link on creating the extension (It is quite easy):

http://devzone.zend.com/article/1021

在MATLAB或Google中搜索"MATLAB C/Fortran API"以获取有关功能的文档.基本上,您可能需要调用EngOpen来调用引擎并返回一个指针.

Search for "MATLAB C/ Fortran API" in MATLAB or google for the documentation on functions. Basically, you'll probably need to call EngOpen to call the engine and return a pointer.

使用engEvalString评估字符串(您可以通过这种方式加载.m文件,也可以执行典型的matlab命令行中可以做的任何事情).

Evaluate a string using engEvalString (you can load .m files this way or do anything you could do in the typical matlab command-line).

当需要查看结果(通常在matlab中输出到命令行的任何内容)时,只需在命令后省略分号,然后使用engOutputBuffer捕获输出即可.

When you need to see the results (anything that is usually output to the command line in matlab), simply omit the semicolon after the command and use engOutputBuffer to capture the output.

这是我写的一些简化示例:

Here is a simplified example from something I wrote:

#include "mat.h"
#include "engine.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define  BUFFER_SIZE 256

int main()

    Engine *ep;
    char buffer[BUFFER_SIZE];   // The buffer used to capture output.

    buffer[BUFFER_SIZE] = '\0'; /* Terminate the last character of the buffer. */

    if (!(ep = engOpen(NULL))) {
        fprintf(stderr, "\nCan't start MATLAB engine\n");
        return EXIT_FAILURE;
    }

    if (engEvalString(ep, "load data/mymatfile.mat") != 0)
    printf("error evaluating expression\n");

    engOutputBuffer(ep, buffer, BUFFER_SIZE);

    /* No output returned. */
    if (engEvalString(ep, "p = 1+1;") != 0)
    printf("error evaluating expression\n");

    /* Output written to buffer- Note the omitted character (;). */
    if (engEvalString(ep, "q = p+1 "))
    printf("error evaluating expression\n");


    /* You will probably need to trim the whitespace in the buffer contents.
    I estimated +5 to pull out the prompt: ">>", but it depends on which version
    you have, for example, the student version displays "EDU >>\n". */
    printf("print the contents of the buffer:%s\n", buffer+5);

    /* Turn off output buffering. */
    engOutputBuffer(ep, NULL, 0);

    /* Close the engine. */
    engClose(ep);

    exit(0);

}

编译完基本的PHP扩展后,将上面对引擎的调用放入扩展中,就可以使用扩展中定义的PHP函数来调用MATLAB.

Once you have got a basic PHP extension compiled, throw the calls to the engine above into your extension and you can call MATLAB using the PHP function that you've defined in your extension.

编译MATLAB API可能是最困难的部分.这是我的Makefile的内容(没有PHP扩展代码).

Compiling the MATLAB API is probably the hardest part. Here are the contents of my Makefile (without the PHP extension code).

phpmat: phpmat.o
        gcc phpmat.o  
/usr/local/matlabR2009a/extern/lib/glnx86/version4.o 
/usr/local/matlabR2009a/bin/glnx86/libeng.so 
/usr/local/matlabR2009a/bin/glnx86/libmex.so -o phpmat

phpmat.o: phpmat.c
        gcc -c phpmat.c -I/usr/local/matlabR2009a/extern/include 
-L/usr/local/matlabR2009a/extern/lib/glnx86 
-L/usr/local/matlabR2009a/bin/glnx86 
-L/usr/local/matlabR2009a/sys/os/glnx86 -L/usr/local/matlabR2009a/bin/glnx86

clean:
        rm *.o

在编译/调用扩展名之前,您可能需要设置LD_LIBRARY_PATH.但是,还有其他替代方法:

You'll probably need to set your LD_LIBRARY_PATH before compiling/ calling the extension... However there are alternatives to this:

LD_LIBRARY_PATH=/usr/local/matlabR2009a/extern/lib/glnx86:/usr/local/matlabR2009a/bin/glnx86:/usr/local/matlabR2009a/sys/os/glnx86:$LD_LIBRARY_PATH

这篇关于我可以在网站上运行MATLAB代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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