时间:2021-07-01 10:21:17 帮助过:26人阅读
通过php调用linux命令,可以实现远程调用matlab.
试验环境:Fedora16, matlab2012b linux version, Google chrome 18.0.1025.108 beta
以下代码实现了在网页输入matlab命令并显示执行结果。注意:如果不加-nodisplay选项,会出现" no display specified"的警告,当然可以在执行matlab命令之前unset DISPLAY也可以直接在terminal执行matlab。
如果你仅仅想远程调用matlab函数,可以参考mathworks的php webservice相关文档,此处也可以参考官方关于python调用的文档。
以下代码对于plot()等绘图命令会出现{Warning: Objects of graph2d.lineseries class exist - not clearing this class or any of its super-classes}错误,目前未解决(也就是无法绘图)。但是通过ssh远程登陆服务器调用相同命令可以plot并save图像,不解。
";//$command="/usr/local/MATLAB/R2012a/bin/matlab -r "."\"".$cmd."\"";shell_exec("unset DISPLAY");//删除DISPLAY环境变量$out = shell_exec($command);$str="< M A T L A B (R) > Copyright 1984-2012 The MathWorks, Inc. R2012a (7.14.0.739) 32-bit (glnx86) February 9, 2012 To get started, type one of these: helpwin, helpdesk, or demo.For product information, visit www.mathworks.com.";$out=ltrim($out,$str);$out=rtrim($out,">>");echo $out;}?>
相关截图:
输入sin(1:10)进行简单计算
输入ver命令获取版本信息
直接终端运行
参考资料:
mathwork关于matlab启动参数的介绍:
http://www.mathworks.cn/help/techdoc/matlab_env/f8-4994.html
SpecifyingMATLAB Startup Options
Youcan specify startup options (also called command flags or commandline switches) that instruct the MATLAB program to perform certainoperations when you start it. On all platforms, you specify theoptions as arguments to the matlab command when you start is at theoperating system prompt. For example, the following starts MATLAB andsuppresses the display of the splash screen.
matlab-nosplash
OnWindows platforms, you can precede a startup option with either ahyphen (-) or a slash (/). For example, -nosplash and /nosplash areequivalent.
Onall platforms, you can also specify startup options using a MATLABstartup file—see Specifying Startup Options in the MATLAB StartupFile
OnWindows platforms, you can specify startup options in the MATLABshortcut—see Including Startup Options in a Shortcut on WindowsSystems.
Youcan add selected startup options (also called command flags orswitches for the command line) to the target path for your shortcutin the Windows environment for MATLAB. For more information about theoptions, see CommonlyUsed Startup Options.
Touse startup options for the MATLAB shortcut icon in a Windowsenvironment, follow these steps:
Right-clickthe shortcut icon for MATLAB andselect Properties from the context menu. TheProperties dialog box for MATLAB opens to the Shortcut pane.
Inthe Target field,after the target path for matlab.exe,add the startup option, and click OK.For example, adding -r"filename" runsthe MATLAB code file filenameafterstartup.
Thisexample instructs MATLAB to automatically run the file results afterstartup, whereresults.m isin the startup folder or on the search path for MATLAB. The statementin the Target fieldmight appear as
C:\Program Files\MATLAB\R2010b\bin\matlab.exe -r "results"
Includethe statement in double quotation marks ("statement").Use only the file name, not the file extension or path name. Forexample, MATLAB produces an error when you run
... matlab.exe -r "D:\results.m"
Usesemicolons or commas to separate multiple statements. This examplechanges the format to short,and then runs the MATLAB code file results:
... matlab.exe -r "format('short');results"
Separatemultiple options with spaces. This example starts MATLAB withoutdisplaying the splash screen, and then runs the MATLAB codefile results:
... matlab.exe -nosplash -r "results"
Atstartup, MATLAB automatically executes the file matlabrc.m and,if it exists,startup.m.The file matlabrc.m,which is in the matlabroot/toolbox/local folder,is reserved for use by MathWorks and by the system manager onmultiuser systems.
Thefile startup.m isfor you to specify startup options. For example, you can modify thedefault search path, predefine variables in your workspace, or definedefaults for Handle Graphics? objects.Use the following statements in a startup.m fileto add the specified folder, /home/username/mytools,to the search path, and to change the current folder to mytools uponstartup.
addpath /home/username/mytoolscd /home/username/mytools
Placethe startup.m filein the default or current startup folder, which is where MATLAB firstlooks for it. For more information, see .
Thefollowing table provides a list of some commonly used startup optionsfor both Windows and UNIX? platforms.For more information, including a complete list of startup options,see the matlab(Windows) referencepage or the matlab(UNIX) referencepage.
Platform | Option | Description |
---|---|---|
All | -clicensefile | Set LM_LICENSE_FILE to licensefile.It can have the form [email protected]. |
All | -hor -help | Displaystartup options (without starting MATLAB). |
All | -logfile"logfilename" | Automaticallywrite output from MATLAB to the specified log file. |
Windowsplatforms | -minimize | StartMATLAB with the desktop minimized. Any desktop tools or documentsthat were undocked when MATLAB was last closed will not beminimized upon startup. |
UNIXplatforms | -nojvm | StartMATLAB without loading the Sun Microsystems JVM? software. Thisminimizes memory usage and improves initial startup speed, butrestricts functionality. With nojvm,you cannot use the desktop, figures, or any tools that requireJava software. Forexample, you cannot set preferences if you start MATLAB withthe -nojvm option.However, you can start MATLAB once without the -nojvm option,set the preference, and quit MATLAB. MATLAB remembers thatpreference when you start it again, even if you usethe-nojvm option. |
All | -nosplash | StartMATLAB without displaying its splash screen. |
All | -r"statement" | Automaticallyrun the specified statement immediately after MATLAB starts. Thisis sometimes referred to as calling MATLAB in batch mode. Filesyou run must be in the startup folder for MATLAB or on the searchpath. Do not include path names or file extensions. Enclose thestatement in double quotation marks ("statement").Use semicolons or commas to separate multiple statements |
All | -singleCompThread | LimitMATLAB to a single computational thread. By default, Windowsmakes use of the multithreading capabilities of the computer onwhich it is running. |
Youcan pass Perl variables to MATLAB on startup by usingthe -r optionof the matlabfunction.For example, assume a MATLAB function test thattakes one input variable:
function test(x)
Tostart MATLAB with the function test,use the command
matlab -r "test(10)"
Onsome platforms, you might need to use double quotation marks:
matlab -r "test(10)"
Thiscommand starts MATLAB and runs test withthe input argument 10.
Topass a Perl variable instead of a constant as the input parameter,follow these steps.
Createa Perl script such as
#!/usr/local/bin/perl $val = 10; system('matlab -r "test(' . ${val} . ')"');
Invokethe Perl script at the prompt using a Perl interpreter.
Formore information, see the matlab(Windows) or matlab(UNIX) referencepage.
Whenthe MATLAB program starts, it constructs the class path for SunMicrosystems Java software using librarypath.txt aswell as classpath.txt.If you call Java software from MATLAB, see more about this in TheJava Class Path and LocatingNative Method Libraries inthe MATLAB External Interfaces documentation.
===============================================================================================================================
http://narnia.cs.ttu.edu/drupal/node/41
StartMATLAB using the command:
matlab -nodisplay -nojvm
Inthis case, you don't even need a MATLAB Prompt to interact with.
matlab -nodisplay -nojvm -r a_code > matlab.out
YourMATLAB program should have the file name a_code.m
(youcan change it to whatever name you like. But when you tell MATLAB torun it, omit the .m
suffix.)The >matlab.out
partredirects the output to a file rather than showing on your Linux/MacOS X terminal. This way is preferred because you have a record onwhat is happening.
First,go to the directory containing the MATLAB script that defines thefunction (or, fancier, add that directory into MATLAB PATH byediting your MATLABPATH environment variable or pathdef.m file).Then, run like this
matlab -nodisplay -nojvm -r "my_function(10)"
Again,you can redirect the output to a file.
Youcan also write a Shell, Perl or Python script to do so. MATLAB givesan example using Perlat http://www.mathworks.com/help/techdoc/matlab_env/f8-4994.html
Writea job script below and submit it using qsub
#!/bin/sh#$ -V#$ -cwd#$ -S /bin/bash#$ -N matlab#$ -o $JOB_NAME.o$JOB_ID#$ -e $JOB_NAME.e$JOB_ID#$ -q normal