首页 > 解决方案 > 如果命令行代码中出现语法错误,如何退出 MATLAB(选项 -r)

问题描述

我需要以批处理模式运行 MATLAB,并希望捕获所有可能的错误。以下代码

matlab -nojvm -nodesktop -nosplash -r "try, x=y; catch ME, disp(getReport(ME)); end; exit"

工作正常,MATLAB 正确地抱怨

Undefined function or variable 'y'.

但是,对于这样的语法错误

matlab -nojvm -nodesktop -nosplash -r "try, x:=y; catch ME, disp(getReport(ME)); end; exit"

MATLAB 说

 try, x:=y; catch ME, disp(getReport(ME)); end; exit
        |
Error: Incorrect use of '=' operator. To assign a value to a variable, use '='. To compare values for equality, use '=='.
>> 

并在命令提示符处暂停。防止这种行为的蛮力方法是

echo "exit" | matlab -nojvm -nodesktop -nosplash -r "try, x:=y; catch ME, disp(getReport(ME)); end; exit"

有没有更优雅的方法来解决这个问题?谢谢。

MATLAB 版本:9.5 (R2018b),操作系统:Linux 4.12.14 (OpenSUSE)

标签: matlab

解决方案


推荐阅读