从命令行运行 MATLAB 脚本

Sheeraz Gul 2022年5月11日
从命令行运行 MATLAB 脚本

扩展名为 .m 的 MATLAB 脚本可以在 Windows 或其他操作系统中使用命令提示符进行编译。

本教程演示如何在 Windows 中从命令行运行 MATLAB 脚本。

从命令行运行 MATLAB 脚本

首先,确保将 MATLAB 添加到环境变量的路径中:

环境变量

将 MATLAB 添加到环境变量后,我们可以通过命令提示符运行它。

让我们尝试从命令提示符运行以下 new.m 文件。

disp('This matlab program is running from command line')

在命令提示符下运行以下命令。在 run() 命令中添加你的路径和文件名。

matlab -nodisplay -nosplash -nodesktop -r "run('C:\Users\Sheeraz\matlab\new.m');

命令行

输出:

MATLAB 命令行窗口

上面的命令将打开带有文件输出的命令窗口,但此命令窗口将保持打开状态。我们可以添加 exit 命令以在成功执行时关闭它。

matlab -nodisplay -nosplash -nodesktop -r "run('C:\Users\Sheeraz\matlab\new.m'); exit

此命令将运行 MATLAB 文件并在成功执行时关闭命令窗口。

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook