Linux环境下运行matlab以及执行m文件

在Linux下安装完matlab后,会在/usr/local/bin/下生成matlab文件,可以使用matlab命令。

在命令行下执行:

$ matlab -help

可以得到帮助文件:

Usage:  matlab [-h|-help] | [-n | -e]
                         [-arch | v=variant | v=arch/variant]
                         [-c licensefile] [-display Xdisplay | -nodisplay]
                         [-nosplash] [-mwvisual visualid] [-debug] [-softwareopengl]
                         [-desktop | -nodesktop | -nojvm]
                         [-r MATLAB_command] [-logfile log]
                         [-Ddebugger [options]]

    -h|-help                          - Display arguments.
    -n                                   - Display final environment variables,
                                            arguments, and other diagnostic
                                            information. MATLAB is not run.
    -e                                   - Display ALL the environment variables and
                                            their values to standard output. MATLAB
                                            is not run. If the exit status is not
                                            0 on return then the variables and values
                                            may not be correct.
    -arch                              - Start MATLAB assuming architecture arch.
    v=variant                       - Start the version of MATLAB found
                                            in bin/glnxa64/variant instead of bin/glnxa64.
    v=arch/variant               - Start the version of MATLAB found
                                            in bin/arch/variant instead of bin/glnxa64.
    -c licensefile                  - Set location of the license file that MATLAB
                                            should use.  It can have the form port@host or
                                            be a colon separated list of license files.
                                            The LM_LICENSE_FILE and MLM_LICENSE_FILE
                                            environment variables will be ignored.
    -display Xdisplay           - Send X commands to X server display, Xdisplay.
    -nodisplay                     - Do not display any X commands. The MATLAB
                                            desktop will not be started. However, unless
                                            -nojvm is also provided the Java virtual machine
                                            will be started.
    -nosplash                      - Do not display the splash screen during startup.
    -mwvisual visualid       - The default X visual to use for figure windows.
    -debug                           - Provide debugging information especially for X
                                            based problems.
    -desktop                        - Allow the MATLAB desktop to be started by a
                                           process without a controlling terminal. This is
                                           usually a required command line argument when
                                           attempting to start MATLAB from a window manager
                                           menu or desktop icon.
    -nodesktop                    - Do not start the MATLAB desktop. Use the current
                                           terminal for commands. The Java virtual machine
                                           will be started.
    -singleCompThread      - Limit MATLAB to a single computational thread.
                                           By default, MATLAB makes use of the multithreading
                                           capabilities of the computer on which it is running.
    -nojvm                           - Shut off all Java support by not starting the
                                           Java virtual machine. In particular the MATLAB
                                           desktop will not be started.
    -jdb [port]                       - Enable remote Java debugging on port (default 4444)
    -r MATLAB_command   - Start MATLAB and execute the MATLAB_command.
    -logfile log                   - Make a copy of any output to the command window
                                           in file log. This includes all crash reports.
    -Ddebugger [options]    - Start debugger to debug MATLAB.
    -nouserjavapath            - Ignore custom javaclasspath.txt and javalibrarypath.txt files.

可以看到与图形界面相关的几个参数是:nodisplay, nosplash, nodesktop, nojvm. 它们分别代表什么含义呢?

  1. nodisplay      不显示任何X命令(X server是Linux下的图形引擎,参考:X Window System)。Matlab桌面环境不会启动,但是会启动Java virtual machine除非使用了nojvm参数;
  2. nosplash   程序启动时不显示启动画面(版权页);
  3. nodesktop  不启动桌面环境,在当前终端中执行命令,但是会启动JVM;
  4. nojvm      关闭java支持,不启动JVM,特别的,desktop也不会启动。

因为Matlab的图形环境依赖JVM,如果不启动JVM,无法执行任何和图形界面相关的命令。若不执行任何X commands,则无法执行imshow()这些函数。所以,我们如果需要在命令行下执行matlab程序,最好只添加:nodesktop nosplash两个参数。

(一)进入m文件所在目录后,运行

$ matlab -nodesktop -nosplash -r matlabfile

只用文件名matlabfile,不能添加.m

若有实现函数参数传入调用,则是如下:

$ matlab -nodesktop -nosplash -r matlabfile(parameters)

(二)也可以在文件.bashrc中添加:

$ vim ~/.bashrc

alias mrun="matlab -nodesktop -nosplash -logfile `date +%Y_%m_%d-%H_%M_%S`.log -r"

其中,logfile `date +%Y_%m_%d-%H_%M_%S`.log 将log文件输出在以程序执行时间为文件名的log文件下。r参数表示运行matlab命令。之后执行m文件只需运行:

$ mrun matlabfile

若有实现函数参数传入调用,则是如下:

mrun matlabfile(parameters)

### 在 Linux 系统中执行 MATLAB 文件的方法 在 Linux 系统中运行 MATLAB 文件可以通过多种方式实现,具体取决于需求以及是否需要将代码转换为独立的可执行文件。以下是几种常见的方法: #### 方法一:通过 MATLAB 命令行界面运行脚本 如果已经安装了 MATLAB,在终端中可以直接启动 MATLAB运行 `.m` 脚本文件。 使用以下命令可以进入 MATLAB 的交互模式并运行指定的脚本: ```bash matlab -nodesktop -nosplash -r "run('your_script.m'); exit;" ``` 上述命令中的选项解释如下: - `-nodesktop`: 不加载完整的桌面环境,仅以命令行形式运行。 - `-nosplash`: 防止显示初始欢迎画面。 - `-r`: 执行后续字符串作为 MATLAB 命令。 此方法适用于简单的脚本运行场景[^1]。 --- #### 方法二:利用 `batch` 功能提交后台作业 对于长时间运行的任务,可以使用 MATLAB 提供的 `batch` 函数来异步执行任务。这特别适合于集群计算环境中。例如: ```matlab job = batch('your_script', 'Pool', 4); % 使用 4 个 worker 运行 your_script.m wait(job); results = load(job.DiaryFile); % 加载日志文件获取结果 delete(job); % 删除 job 对象释放资源 ``` 这种方式允许用户在不阻塞当前会话的情况下完成复杂运算。 --- #### 方法三:编译 MATLAB 代码为独立的 Linux执行文件 当希望脱离 MATLAB 环境运行程序时,可以选择将其编译为独立的应用程序或库。MATLAB Compiler (`mcc`) 是用于这一目的的主要工具。 ##### 步骤说明: 1. **准备开发环境**:确保已安装支持编译功能的 MATLAB 版本(如引用提到的 2020a 或更高版本)。同时确认目标机器上也安装了 MATLAB Runtime (MCR)[^3]。 2. **编写入口函数**:创建一个带有清晰输入输出接口的主函数文件(假设名为 `my_matlab_function.m`)。 3. **执行编译指令**:根据实际需求调整参数后发出相应命令。比如生成 C++ 库的形式: ```bash mcc -W cpplib:mylib -T link:lib -d ./output_dir my_matlab_function.m ``` 4. **部署产物至其他设备**:将生成物连同必要的依赖项一起分发给最终用户的计算机,并按照官方文档指导设置好路径变量 `$LD_LIBRARY_PATH` 等配置信息[^5]。 注意:不同类型的输出可能涉及额外步骤或者特定限制条件,请参照 MathWorks 官方资料进一步了解详情[^4]。 --- ### 示例代码片段展示如何简单调用外部 MEX 文件 假如我们有一个自定义扩展名 `.cpp` 编写的算法模块,则需先完成构建过程再正常导入项目里测试效果。 ```c++ #include "mex.h" void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { double input_value; /* Check for proper number of arguments */ if(nrhs !=1 ) { mexErrMsgIdAndTxt( "MATLAB:mynumargs", "One input required."); } else if(!mxIsDouble(prhs[0]) || mxIsComplex(prhs[0])){ mexErrMsgIdAndTxt( "MATLAB:inputNotRealScalarDouble","Input must be a real scalar double.");} // 获取传入数值 input_value=mxGetScalar(prhs[0]); plhs[0]=mxCreateDoubleMatrix((mwSize)1,(mwSize)1,mxREAL); *(double *)mxGetData(plhs[0])=(input_value*2)+7; // 计算逻辑演示 } ``` 以上展示了基本框架结构,更多高级特性参阅链接地址提供的升级指南页面内容。 ---
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值