Doug's MATLAB Video Tutorials

February 3rd, 2009

Debugging session

clear
num = [1 1 1 1 1 1 1];
den = [1 2 3 0 5 6 7];
for i = 1: numel(den)
    if den(i) == 0
        a = 1
    else
        a(i) = num(i)/den(i)
    end
end
plot(a,'o-')
The above code does not behave as expected. The values in the middle of the vector are zeroed out. This debugging session figures out why this happens.
Find the files here.

3 Responses to “Debugging session”

  1. Dennis replied on :

    The published code is actually the fixed version.

  2. Christos Saragiotis replied on :

    Nice example, point taken.

    However, the description of the video is misleading. It says “the above code does not behave as expected”, while the posted code is the corrected one.

    Regards

  3. dhull replied on :

    D’oh!

    Problem “unsolved”. First time I have purposefully put a bug back into my code.

    -Thanks,
    Doug

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


MathWorks

Doug Hull is a proud MathWorker who is on a mission to help you with MATLAB.

Doug's picture

These postings are the author's and don't necessarily represent the opinions of The MathWorks.