MATLAB: Coder, randsample: Variable ‘edges’ is not fully defined on some execution paths.

MATLABmatlab coderrandomvariable

I'm trying to compile a Matlab code with the coder that calls randsample(n,k,true,w) for integers n and k and a weight-vector w. I get the error message, "Variable 'edges' is not fully defined on some execution paths." which seems to be a problem within the randsample function. Any ideas what to do without touching randsample.m itself? Thanks

Best Answer

This is a bug in the code generation version of RANDSAMPLE. The compiler is complaining about a situation that it really doesn't need to complain about, as the edges variable is never referenced unless it is first defined as far as I can tell. It's just challenging to infer that from a static analysis.
I'll create an internal bug report for this to get it fixed. It's really just a matter of providing an initialization for edges even when w is empty (e.g. adding
else
edges = zeros('like',w);
before the "end" on line 66 of matlab/toolbox/stats/eml/randsample.m. I mean, that's completely unsupported, and I'm not recommending it. You'd be doing that at your own risk. Really. Who knows what might happen?).
Related Question