声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

声振论坛 展示 基础理论 查看内容

使用php在网页执行matlab

2012-10-18 06:57| 发布者: aspen| 查看: 14264| 评论: 0|原作者: Jillian|来自: 振动论坛

摘要: 目录 IncludingStartup Options in a Shortcut on Windows Systems SpecifyingStartup Options in the MATLAB Startup File CommonlyUsed Startup Options PassingPerl Variables on Startup Startupand Callin ...
Linux下后台运行matlab  
命令如下:
  1. nohupmatlab name.out &
复制代码

其中name.m是你自己的M文件.
更完整的写法应该是:
  1. nohupmatlab < name.m >& name.out &
复制代码

同样:matlab< name.m >& name.out & 就够了,不用nohup。

==============================================================================
UsingMATLAB with PHP
http://www.danpearce.co.uk/php_and_matlab
Thefollowing example describes how to use PHP to run MATLAB commands.With there being so many different possible system configurations,it is likely that you may have to modify these steps slightly foryour own use. I am using:

MATLABR2009aWindowsVista
Apache2.2.11

Inthis example, we will create a MATLAB function that generates a textfile of random numbers. The location of this file is user definedfrom an HTML form. We use PHP to link the two together.

The MATLABfile
First,we need create a very basic MATLAB function that creates a plaintext file. The only input is a filename. Obviously, you'd want someslightly more robust checks normally. Using any plain text editor,copy and paste the following code and save as phpcreatefile.m (makea note of the file path as you'll need it later):

  1. function phpcreatefile(filepath)
  2. % Open the file
  3. fid = fopen(filepath, 'wt');
  4. for i = 1:100
  5.     % Create random number
  6.     randNumber = [num2str(rand(1)) '\n'];
  7.     % Write number to file
  8.     fprintf(fid, randNumber);
  9. end
  10. % Close file
  11. fclose(fid);
  12. % Quit MATLAB
  13. quit force
复制代码

Noticethe last line of this file. The 'Quit' command is necessary toterminate MATLAB when we're finished.

The PHP file
Next,we need to create a file that renders an HTML form that, whensubmitted, opens MATLAB and calls the function phpcreatefile with afilename taken from the form. copy and paste the following code intoa php file:


  1.    
  2.         

  3.             Enter a filename

  4.             

  5.         

  6.    


  7. if(isset($_POST['filepath'])) {
  8.     $filename  = $_POST['filepath'];
  9.     $inputDir  = "C:\\output";
  10.     $outputDir = "C:\\output";
  11.     $command = "matlab -sd ".$inputDir." -r phpcreatefile('".$outputDir."\\".$filename."')";
  12.     exec($command);
  13.     echo "The following command was run: ".$command."
    ";
  14.     echo $filename." was created in ".$outputDir."
    ";
  15. }
  16. ?>
复制代码

Theabove code first creates an extremely basic HTML form that asks theuser for a filename. When submitted, the form submits to self,making the filename available as a $_POST variable. Next, a block ofPHP code checks to see if the form is submitted. If it is, we startto try and build the appropriate command to send to MATLAB.

    1. $filename= the filename submitted by the user from our form
    2. $inputDir= the path to the folder that you saved phpcreatefile.m in (this isonly really necessary if the function phpcreatefile is saved in afolder that is not included in PATH)
    3. $outpurDir= the path to the folder we are going to create our text file in
    4. $command= Here, we're building a command line string required to runMATLAB. Lets break it down a bit further:
            matlab= the command we pass to CMD in order to start MATLAB
            -sd"startdir" specifies the startup directory for MATLAB:in this case we set it to match the location of our functionphpcreatefile
           -r"statement" starts MATLAB and executes the specifiedMATLAB statement.
    5. exec($command)executes our command line string as if you'd entered it in thecommand window
    6. Nextare a couple of lines that report back to our user what MATLAB hasjust done

Thatsit. You just ran MATLAB using PHP

Foundthis useful? Do you have comments or possible improvements to thisexample? Please leave your comments at the bottom.

Notes
Onething that is immediately apparent is that at no time can MATLAB beseen running while the above code executes. This is because PHP runsas a module of an Apache service. Vista (and maybe other systemstoo) has a security layer that deals with how services interact withthe desktop. The problem is better described here.

本文内容由 Jillian 提供

最新评论

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-4-19 00:59 , Processed in 0.034067 second(s), 16 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部