Matlab window example broken

This is minor. I was just doing the Matlab window tutorial and got the following error:

Error: Integers can only be combined with integers of the same class, or scalar doubles. (MATLAB:mixedClasses)
In linspace.m at 30
In hist.m at 94
In matlab_window_demo.m at 22

I figured out there are two reasons for this error: First, MWorks is making rand_var an int64 (or that’s how Matlab is seeing it at least). This still happens even if I define rand_var as an integer. I have to use MWorks’ rand() if I want Matlab to get doubles. This is a problem because the default bin centers for Matlab’s hist() aren’t integers, which causes the error. However, setting the bin centers to integers (1:10), I get a different error:

Error: First Input type not supported (MATLAB:histc:InvalidFirstNumericInput)
In hist.m at 119
In matlab_window_demo.m at 22

Apparently hist() doesn’t like int64 objects at all. So there are two solutions, ignoring the option of changing disc_rand() to rand() in the .xml file:

  1. Have the m-file recast to a double or an integer type that the function you want to use likes.
  2. Figure out a way to get MWorks to output a variable as a double even if the values are all (mathematically) integers.

I have no idea how difficult it would be to do #2, and it might break current functionality, so I’m guessing #1 is the better of the two.

Hi Evan,

Thanks for reporting this. I made the change that introduced this issue quite a while ago, but I failed to update the tutorial.

Luckily, this problem is easy to fix. To fix it in the .m file, replace

hist(values);

with

hist(cast(values, 'double'));

To fix it in the .xml file, replace

disc_rand(1, 10)

with

(double)disc_rand(1, 10)

I prefer and recommend the first option, so I’ve posted an updated version of the .m file to the tutorial.

Cheers,
Chris