23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

8 Error Handling<br />

end<br />

function f2(x)<br />

f3(x-1)<br />

function f3(y)<br />

x = log(y);<br />

end<br />

end<br />

end<br />

After enabling all warnings, run the M-file. The code generates a “Log of zero”<br />

warning. In an M-file of this size, it is not difficult to find the cause of the<br />

warning, but in an M-file of several hundred lines, this could take some time:<br />

warning on all<br />

f1(50,1)<br />

Warning: Log of zero.<br />

To simplify the debug process, enable backtrace mode. In this mode, <strong>MATLAB</strong><br />

reports which function generated the warning (f3), the line number of the<br />

attempted operation (line 8),thesequenceoffunctioncallsthatleduptothe<br />

execution of the function (f1>f2/f3), and the line at which each of these<br />

function call was made (3 and 6):<br />

warning on backtrace<br />

f1(50,1)<br />

Warning: Log of zero.<br />

> In f1>f2/f3 at 8<br />

In f1>f2 at 6<br />

In f1 at 3<br />

Example 2 — Enabling Verbose Warnings<br />

When you enable verbose warnings, <strong>MATLAB</strong> displays an extra line of<br />

information with each warning that tells you how to suppress it:<br />

Turn on all warnings, disable backtrace (if you have just run the previous<br />

example), and then enable verbose warnings:<br />

warning on all<br />

warning off backtrace<br />

8-24

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

Saved successfully!

Ooh no, something went wrong!