MATLAB - 錯誤處理



錯誤處理是一種允許您管理和響應在 MATLAB 程式執行期間可能發生的錯誤或意外情況的方法。與其讓程式崩潰並給出不必要的結 果,不如優雅地處理錯誤,向用戶提供反饋,甚至嘗試從問題中恢復。

執行程式碼時可能發生的常見錯誤型別包括語法錯誤、執行時錯誤或邏輯錯誤。

語法錯誤

MATLAB 中的語法錯誤發生在程式碼的結構或語法存在錯誤時。

示例

% Syntax Error Example
c = a + b;

在此程式碼中,存在語法錯誤,因為在計算中使用 a 和 b 之前,它們未定義或未賦值。MATLAB 期望在表示式中使用這些變數之前定義或分配它們的值。要更正此語法錯誤,應在使用 a 和 b 之前定義或分配它們的值。

執行時錯誤

MATLAB 中的執行時錯誤發生在程式碼正在執行但執行過程中遇到阻止其成功完成的問題時。

result = 10 / 0; % Division by zero

嘗試將數字除以零會導致執行時錯誤。

邏輯錯誤

% Logical Error Example
n = 5;
sum = 0;
for i = 1:n
   sum = sum + i;
end

在此程式碼中,迴圈從 1 執行到 n,將 1 到 n 的值新增到 sum 中。但是,如果我們想要計算 1 到 n 的數字之和,則程式碼不正確,因為它會將 n 包括在 sum 中。迴圈應該從 1 執行到 n-1。

matlab 中的錯誤處理有助於我們在執行程式碼時處理任何語法、執行時和邏輯問題。錯誤處理會將任何程式碼執行失敗的正確原因告知使用者。

在 Matlab 中向用戶顯示錯誤訊息

以下是如何在 matlab 中使用顯示錯誤訊息向用戶顯示錯誤訊息的簡單示例:

語法

error(msg) : helps to generate a simple error message with a custom explanation
error(msg,A) : helps to create an error message with placeholders (e.g., %s, %d) that can be replaced with actual values from variables.

示例 1:使用 error(msg)

value = 120;
if value > 100
   error('Value entered is greater than expected. Please use a smaller value.');
end

在此示例中,如果值為大於 100,它將觸發帶有自定義訊息“輸入的值大於預期。請使用較小的值。”的錯誤。

在 matlab 命令視窗中執行時,輸出為:

>> value = 120;
if value > 100
   error('Value entered is greater than expected. Please use a smaller value.');
end

error: Value entered is greater than expected. Please use a smaller value.

示例 2:使用 error(msg, A)

age = 150;
if age > 100
   error('Error: Age value is too high. It should be below %d.', 100);
end

在此示例中,如果年齡大於 100,則錯誤訊息將包括值 100 以代替 %d 佔位符,從而導致訊息:“錯誤:年齡值過高。它應該低於 100。”

在 matlab 命令視窗中執行時,輸出為:

>> age = 150;
if age > 100
    error('Error: Age value is too high. It should be below %d.', 100);
end

error: Age value is too high. It should be below 100.

在 Matlab 中向用戶顯示警告訊息

對於警告訊息,我們可以使用 matlab 中提供的內建函式 warning()。

warning(msg) : used to issue a warning with a custom message msg.
warning(msg,A) : displays message containing format conversion characters, similar to the ones used in MATLAB sprintf() function.Every placeholder character within the 'msg' is substituted with one of the corresponding values from 'A'.
warning(state): enables, disables, or displays the status of all warnings.

示例 1

在下面的示例中,將顯示一條警告訊息,內容為:“x 的值 (10) 大於 5。”。%d 是一個格式化字元,x 的值將插入到該位置的警告訊息中。

x = 10;
if x > 5
   warning('The value of x (%d) is larger than 5.', x);
end

在 matlab 命令視窗中執行時,輸出為:

>> x = 10;
if x > 5
   warning('The value of x (%d) is larger than 5.', x);
end

Warning: The value of x (10) is larger than 5.

示例 2

讓我們使用 warning() 方法僅顯示警告訊息,如下例所示:

warning('Testing warning!')

在 matlab 命令視窗中執行時,輸出為:

>> warning('Testing warning!')

Warning: Testing warning! 

示例 3

要了解警告的狀態,您只需在 matlab 命令視窗中鍵入 warning 並回車即可。

warning

在 matlab 命令視窗中執行時,輸出為:

>> warning
All warnings have the state 'on'.

示例 4

warning('off')

執行上述程式碼並鍵入 warning 後,您應該會看到訊息“所有警告的狀態均為 'off'。”。

>> warning('off')
>> warning
All warnings have the state 'off'.

示例 5

warning('on')

執行上述程式碼並鍵入 warning 後,您應該會看到訊息“所有警告的狀態均為 'on'。”。

>> warning('on')
>> warning
All warnings have the state 'on'.

使用 try/catch 語句進行錯誤處理

MATLAB 中的 try 和 catch 塊有助於管理程式碼執行期間可能發生的潛在錯誤。

try/catch 示例

該示例顯示了嘗試在 try 塊內執行 test() 函式的程式碼。如果在執行 test() 期間發生錯誤,MATLAB 將捕獲錯誤並跳轉到 catch 塊,在該塊中,它將顯示一條錯誤訊息,其中包含有關錯誤的詳細資訊,使用 disp(['Error occurred: ' exception.message]);

try
   a = test()
catch exception
   % Handle the error here
   disp(['Error occurred: ' exception.message]);
end

在 matlab 命令視窗中執行相同的操作時,輸出為:

>> try
   a = test()
catch exception
   % Handle the error here
   disp(['Error occurred: ' exception.message]);
end
Error occurred: Execution of script test as a function is not supported:
/MATLAB Drive/test.m

如果您檢視執行情況,則函式 test 不存在,因此捕獲錯誤並顯示詳細的錯誤訊息。

廣告

© . All rights reserved.