using codegen hist(z, sqrt(length(z)) error scalar X must be constant

I use codegen to generate C code from Matlab code. in a Matlab sub-function, I have a line of code:
[nz, bins] = hist(z, sqrt(length(z)));
error message underlines this line, saying "scalar X must be constant". Does anyone know how to make it work the way it is designed but not violating codegen rules?
Thanks a lot!

 Accepted Answer

You will need to substitute code.
As you are passing the number of bins rather than the bin centers, the bins would be created as equal width between min(z(:)) and max(z(:)) of the values. You can use linspace() to generate the edge values and then call histc() . Just watch out for the behavior of histc() on the exact upper bound, so you might want to substitute inf() for the upper bound. Though if you do substitute inf for the upper bound, make sure your code generation settings permit inf as some of the optimizations recommend not permitting it, especially if you are using the Fixed Point Toolbox and targetting HDL.

Asked:

RW
on 31 Oct 2016

Answered:

RW
on 31 Oct 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!