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

조회 수: 2 (최근 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
Michael Hartmann
Michael Hartmann 2018년 8월 2일
I just wanted to add, adding the suggested two lines to RANDSAMPLE worked in my case.
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