从命令行运行 MATLAB 脚本

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

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

从命令行运行 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 脚本

输出:

从命令行运行 MATLAB 脚本

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

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

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