Coder, randsample: Variable 'edges' is not fully defined on some execution paths.

1 次查看(过去 30 天)
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
  3 个评论
Stephen23
Stephen23 2018-7-31
编辑:Stephen23 2018-7-31
@Michael Hartmann: please upload your code by clicking on the paperclip button.
'"Variable 'edges' is not fully defined on some execution paths." which seems to be a problem within the randsample function'
Interesting. We need to see your code.
Michael Hartmann
Michael Hartmann 2018-7-31
Attached is the code that calls randsample, where the input is specified as
N = 3;
Mk = 10;
Mp = 10;
s_length = floor(200000);
x = 0.01*rand((N+1)*(Mk+Mp+1)*2,1);

请先登录,再进行评论。

采纳的回答

Mike Hosea
Mike Hosea 2018-7-31
编辑:Mike Hosea 2018-7-31
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?).
  2 个评论
Mike Hosea
Mike Hosea 2018-8-2
Glad to hear it. FYI, 2018b development is currently past the point where we could reasonably slip this in. Just wanted you to know in case you upgrade to 18b and expect to see the fix there.

请先登录,再进行评论。

更多回答(1 个)

Joel Fernandez
Joel Fernandez 2018-8-6
编辑:Stephen23 2018-8-6
Hi everyone Can someone help me ? My code error is: "Variable 'sa' is not fully defined on some execution paths" So I'm using this code to SVPWM
function sf =aaa(u)
ts=0.0002;vdc=1;peak_phase_max= vdc/sqrt(3);
x=u(2); y=u(3);
mag=(u(1)/peak_phase_max) * ts;
%sector I
if (x>=0) & (x<pi/3)
ta = mag * sin(pi/3-x);
tb = mag * sin(x);
t0 =(ts-ta-tb);
t1=[t0/4 ta/2 tb/2 t0/2 tb/2 ta/2 t0/4];
t1=cumsum(t1);
v1=[0 1 1 1 1 1 0];
v2=[0 0 1 1 1 0 0];
v3=[0 0 0 1 0 0 0];
for j=1:7
if(y<=t1(j))
break
end
end
sa=v1(j);
sb=v2(j);
sc=v3(j);
end
sf=[sa, sb, sc];
Thanks
  1 个评论
Stephen23
Stephen23 2018-8-6
编辑:Stephen23 2018-8-6
@Joel Fernandez: your code is badly aligned. Badly aligned code is one way that beginners hide bugs in their code. You should align your code using the default MATLAB editor settings. You can align the code: select all code, then click ctrl+i. It will give this:
function sf = aaa(u)
ts = 0.0002; vdc = 1; peak_phase_max = vdc / sqrt(3);
x = u(2); y = u(3);
mag = (u(1) / peak_phase_max) * ts;
%sector I
if (x >= 0) & (x < pi / 3)
ta = mag * sin(pi / 3 - x);
tb = mag * sin(x);
t0 = (ts - ta - tb);
t1 = [t0 / 4 ta / 2 tb / 2 t0 / 2 tb / 2 ta / 2 t0 / 4];
t1 = cumsum(t1);
v1 = [0 1 1 1 1 1 0];
v2 = [0 0 1 1 1 0 0];
v3 = [0 0 0 1 0 0 0];
for j = 1:7
if (y <= t1(j))
break
end
end
sa = v1(j);
sb = v2(j);
sc = v3(j);
end
sf = [sa, sb, sc];
This makes it clear that if x<0 or x>pi/3 or any of the y>t1 then sa, sb and sc will not be defined, thus the error message.
Note that using lots of superfluous whitepsace in this vector has made it unclear what it should contain:
[t0 / 4 ta / 2 tb / 2 t0 / 2 tb / 2 ta / 2 t0 / 4];
It is clearer to use commas instead of whitespace to separate the array elements, and to not use whitespace around operators, e.g.:
[t0/4, ta/2, tb/2, t0/2, tb/2, ta/2, t0/4];

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB Coder 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by