0

When I execute my script, one of the lines of code sets a variable to a 2x2 array of zeros. If I set a breakpoint, highlight the line and execute it in the command window, it produces an integer (which it should).

The other line sets first_peak to 0, when it should be 667.

(I added the cast in an attempt to resolve the problem because matlab was complaining about the variable type. Of course it didn't work.)

I can't seem to create an MWE without one of my datafiles, so attached are screenshots.

in-script:

in-script

command window:

command window

If an MWE were going to produce the error, this one should, but again, it doesn't. The peaks(peak_ndx+1) line would produce the aforementioned 2x2 array, and peaks(peak_ndx) would produce the 0 value.

clear
for ndx = 1 : 6
    peaks = [667 911 1288 1719 2114 2363 3505 3718 4010 4372 4682 4867];
    peak_ndx=(ndx - 1) * 2 + 1;
    peaks(peak_ndx)
    peaks(peak_ndx+1)
end
4
  • You need to have a piece of code that reproduce the problem. Nothing in the sample code seems to generate a 2x2 matrix. But your issue may be a simpler one: peaks is a function. It is wise to change your array name. Commented Oct 2, 2017 at 23:31
  • "I can't seem to create an MWE without one of my datafiles, so attached are screenshots." Commented Oct 3, 2017 at 1:01
  • Didn't know peaks is a function. doc peaks just said it is a variable. Will have a look. Commented Oct 3, 2017 at 1:02
  • That's because you've overshadowed the built-in peaks function. Try clear peaks; doc peaks Commented Oct 3, 2017 at 9:29

2 Answers 2

2

TL;DR

When you hover the mouse over a variable, it shows the value that the variable currently has in the workspace.


The answer lies in the first paragraph you wrote. As you have mentioned, "When I execute my script, one of the lines of code sets a variable to a 2x2 array of zeros.", therefore hovering over second_peak on the line second_peak = cast(peaks(peak_ndx+1), 'int32') shows you zeros. Note that you have put a breakpoint on this line and this line is not executed yet. When you execute that line (unpause/continue running from the breakpoint) or enter that in the command window, you get the desired result.

If you put second_peak on another line and put a breakpoint on that line. Then after the second_peak = cast(peaks(peak_ndx+1), 'int32') line is run and program execution is paused at the next breakpoint, you'll see 1x1 int32 911.

Here is a reproducible example for you:

second_peak = int32([0,0;0,0]); %initially the value that you had
second_peak = int32(911);       %put a breakpoint here
second_peak                     %and also here

graphics

Also make it sure that you're not doing unnecessary preallocation here.
Read "A Common Misunderstanding in Array Preallocation" in Loren Shure's blog.

Sign up to request clarification or add additional context in comments.

1 Comment

The breakpoint I had set was after second_peak was set to peaks(). That was the line of code I was talking about in my post, and I didn't word that too well.
0

If a variable is declared with the same name as a function, the variable may mask the function in some contexts (such as the Command Window), but not in others, such as in a script.

1 Comment

The variable always overshadows the built-in function, both in script and the Command Window. There are no ifs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.