23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Program Control Statements<br />

while loop when the first empty line is encountered. The resulting character<br />

array contains the M-file help for the fft program.<br />

fid = fopen('fft.m', 'r');<br />

s = '';<br />

while ~feof(fid)<br />

line = fgetl(fid);<br />

if isempty(line)<br />

break<br />

end<br />

s = strvcat(s, line);<br />

end<br />

disp(s)<br />

Error Control — try, catch<br />

Error control statements provide a way for you to take certain actions in the<br />

event of an error. Use the try statement to test whether a certain command<br />

in your code generates an error. If an error does occur within the try block,<br />

<strong>MATLAB</strong> immediately jumps to the corresponding catch block. The catch<br />

part of the statement needs to respond in some way to the error.<br />

try and catch<br />

The general form of a try-catch statement sequence is<br />

try<br />

statement<br />

...<br />

statement<br />

catch<br />

statement<br />

...<br />

statement<br />

end<br />

In this sequence, the statements between try and catch are executed until<br />

an error occurs. The statements between catch and end are then executed.<br />

Use lasterr to see the cause of the error. If an error occurs between catch<br />

and end, <strong>MATLAB</strong> terminates execution unless another try-catch sequence<br />

has been established.<br />

3-95

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!