MATLAB 程式設計/在 Disp Warn 和 Error 中插入換行符
外觀
函式 warning、 error、 sprintf 和 fprintf 會將 '\n' 解釋為換行符。例如
>> error('This error\nhas a newline.')
??? This error
has a newline.
雖然該維基百科的先前版本聲稱此功能是在 MATLAB 6.5 (R13) 中引入的,但在 7.4.0 (2007a) 中它不起作用。該版本 發行說明 中關於此更改發生在格式化錯誤字串引入時的解釋並沒有幫助。
要在上述示例不起作用的版本中新增換行符,請使用 SPRINTF 或 CHAR(10)
>> error(sprintf('This error\nhas a newline.'))
??? This error
has a newline.
>> disp(['abcd' char(10) 'efgh'])
abcd
efgh
這也有效
>> disp(['abcd', 10, 'efgh'])
abcd
efgh
在 MATLAB 2016b 及更高版本中,建議使用 NEWLINE 函式,以提高程式碼清晰度
>> disp(['abcd' newline 'efgh'])
abcd
efgh