winggundam
Would you like to react to this message? Create an account in a few clicks or log in to continue.

TRY-CATCH區塊(TRY-CATCH BLOCKS)

向下

TRY-CATCH區塊(TRY-CATCH BLOCKS) Empty 回復: TRY-CATCH區塊(TRY-CATCH BLOCKS)

發表 由 lung 周一 11月 28, 2011 12:25 pm

Try-Catch區塊提供使用者控制偵錯(error-trapping)的能力。也就是,在Try-Catch區塊內,由MATLAB發現的錯誤會被捕捉,給予使用者控制MATLAB如何回應錯誤的能力。Try-Catch區塊有以下型式

try
(command1)
catch
(command2)
end

在此所有在(command1)裡的MATLAB表示式都會被執行。如果沒洧MATLAB錯誤產生,控制權會傳遞給end陳述式。然而,如果當執行(command1)時出現了一個MATLAB錯誤,控制權立即傳到catch陳述式且執行接下來的表示式(command2)。在catch區塊中,函式lasterr包含由在try區塊裡遇到的錯誤所產生的字串。結果,catch區塊表示式(command2)可取出錯誤字串並且根據其內容而動作。

考慮下例為了方便在script M–file裡寫的例子。

x=ones(4,2);
y=4*eye(2);
try
z=x*y;
catch
z=nan;
disp(&#039X and Y are not conformable.&#039)
end
z

以上面和y的資料,此程式碼片段會產生如下的輸出

z =
4 4
4 4
4 4
4 4

在此例中只有在try區塊中的程式碼被執行,改變變數y來製造一個錯誤。

x=ones(4,2);
y=4*eye(3); % 現在y的大小有誤
try
z=x*y;
catch
z=nan;
disp(&#039X and Y are not conformable.&#039)
end
z

執行此刻的程式碼會在Command window中產生下例的輸出。
X and Y are not conformable.
z =
NaN

此外,函式lasterr描述所找到的錯誤。

>> lasterr
ans =
Error using ==> *
Inner matrix dimensions must agree.
lung
lung
Admin

文章數 : 26067
注冊日期 : 2009-07-12

回頂端 向下

TRY-CATCH區塊(TRY-CATCH BLOCKS) Empty 回復: TRY-CATCH區塊(TRY-CATCH BLOCKS)

發表 由 lung 周一 11月 28, 2011 12:25 pm

clear
a=magic(4);
b=eye(3);
try
c=a*b %執行該語句段出現錯誤,轉而執行catch之後的語句段
catch
c=a(1:3,1:3)*b
end
lasterr
lung
lung
Admin

文章數 : 26067
注冊日期 : 2009-07-12

回頂端 向下

TRY-CATCH區塊(TRY-CATCH BLOCKS) Empty 回復: TRY-CATCH區塊(TRY-CATCH BLOCKS)

發表 由 lung 周一 11月 28, 2011 12:25 pm

舉例一

try

img = imread(filename_path);

imgin = double(img)./double(max(img(:)));

clear img

if flag_function

imgout = feval(strtok(handles.data_your_function,' '),imgin);

end

if ~(exist('imgout'))

imgout = imgin;

clear imgin;

end

[r,c,m] = size(imgout);

imgout = imgout ./ max(imgout(:));

if m == 1

figure, colormap(gray), imagesc(imgout .^ handles.data_gamma),axis image;

drawnow

else

figure, imagesc(imgout .^ handles.data_gamma),axis image;

drawnow

end

catch

%results_wild('results_text_outside_Callback',gcbo,[],guidata(gcbo),101,lasterr,handles);
results_text_outside_try(lasterr,handles);

disp(lasterr);

end

clear filename_path imgout;

end

舉例2

movie(time_lapse_movie,1,fpsval);

for indtemp = 1:prod(size(handles.data_selected_path_list)),

%filename_path = strcat(strrep(handles.data_selected_path_list{indtemp},'D:','\\CASTOR\D Drive'),'\thumbnail.jpg');
%filename_path = strcat(strrep(handles.data_selected_path_list{indtemp},'D:',''),'\thumbnail.jpg');
filename_path = strcat(handles.data_selected_path_list{indtemp},'\thumbnail.jpg');

%if ~exist(filename_path,'file')
% filename_path = strrep(filename_path,'\','/');
%end


try

img = imread(filename_path);

time_lapse_movie(indtemp) = im2frame(img);

clear img;

catch

disp(lasterr);

%results_wild('results_text_outside_Callback',gcbo,[],guidata(gcbo),101,lasterr,handles);
results_text_outside_try(lasterr,handles);

end
lung
lung
Admin

文章數 : 26067
注冊日期 : 2009-07-12

回頂端 向下

TRY-CATCH區塊(TRY-CATCH BLOCKS) Empty 回復: TRY-CATCH區塊(TRY-CATCH BLOCKS)

發表 由 lung 周一 11月 28, 2011 12:25 pm

Try
這個網頁介紹的語句都不是很大的重點, 知道它是幹嘛的, 需要用的時候仔細看看就行了.
先看'try', try的作用是讓Matlab嘗試執行一些語句, 執行過程中如果出錯, 則執行catch部分的語句. 其語法:

try
嘗試執行的語句塊
catch
出錯後執行的語句塊
end

如果出錯, 就中斷正在執行的'嘗試執行的語句塊', 跳到'出錯後執行的語句', 當然, 在'出錯後執行的語句塊'裏面, 你可以設置'resume'命令, 告訴matlab怎樣從錯誤中恢復過來. resume的基本用法請參照其幫助文檔.

input
input命令用於程序執行過程中要求用戶想程序中輸入數據. 其用法:

變量名=input(提示信息字符串)

, 執行到這個命令時, Matlab的命令窗口中將顯示'提示信息字符串', 此時用戶可以輸入數據, 這個數據將被賦值給此語句中指定的'變量名'. 例如: mydata=input('請輸入mydata這個變量的值');

return
return主要用於以下兩處, 第一用於函數中, 函數將在此處跳出, 並返回該語句指定的值. 其二, 用於keyboard狀態下返回被keyboard中斷的指令處繼續向後執行程序.

keyboard
這個命令在調試程序時非常有用, 當執行到此命令, matlab將中斷正在執行的程序, 進入'用戶控制狀態', 可以在command window中執行用戶操作---一般我都用這種辦法來觀察程序執行過程中間的狀況. 這個狀態將一直持續, 直到輸入'return'命令, 才回到剛才中斷的程序處, 繼續向後運行

pause
程序執行途中暫停. 語法:

pause(n)

暫停n秒鐘, 一般情況下可以精確到0.01秒.

error
終止程序運行並給出出錯信息:

error('出錯信息')

利用另外一個命令: lasterr可以得到上一次出錯的信息.

warning
給出警告信息, 程序繼續運行:

warning('警告信息')

另一個相關命令: lastwarn得到上一次警告信息.
lung
lung
Admin

文章數 : 26067
注冊日期 : 2009-07-12

回頂端 向下

回頂端


 
這個論壇的權限:
無法 在這個版面回復文章