MatLab Coder: MatLab to C - Output argument not assigned on some execution paths

2 views (last 30 days)
I am trying to run MatLab Coder to transform a matlab .m file into .mex to hopefully run the code more quickly. When trying to build the file via MatLab Coder engine, I get the following error:
Output argument 'summstat' is not assigned on some execution paths
Where summstat is the output argument of the funtion I am trying to compile. In the last line of my code I assign summstat:
summstat = pinc;
where pinc is another variable used throughout the code. Most of the problems of this type occurs when the output variable is assigned inside an if-loop, but this is not the case here. Although I do have ifs and fors throughout the code, the assignment is not inside any of these (like I said, it is the last line of the funcion).
Any help is appreciated.
Here is the code:
function summstat = fun(para,T)
coder.extrinsic('clock');
summstat = 0;
pinc = 0;
prob = [ .8 .2; .5 .5];
A = 1.00;
theta = 0.05;
Kstart = 10.0;
g = 0.20;
sigma = para(1);
beta = para(2);
delta = para(3);
alpha = para(4);
maxkap = 20;
inckap = 0.025;
nkap = round(maxkap/inckap+1);
D = zeros(length(prob));
[ev,ed] = eig(prob);
[emax,inmax] = max(diag(ed));
if emax~=1;
disp('are you sure the matrix prob is correct?');
end;
D(inmax,inmax) = real(emax);
pinf = ev*D*inv(ev);
pempl = pinf(inmax,inmax);
N = 1.0*pempl + theta*(1-pempl);
liter = 1;
maxiter = 50;
toler = 0.001;
metric = 10;
K = Kstart;
Kold = 0;
nstates = size(prob,1);
decis = zeros(nkap,nstates);
rent=0;
wage=0;
while (metric > toler) & (liter <= maxiter);
wage = (1-alpha) * A * K^(alpha) * N^(-alpha);
rent = (alpha) * A * K^(alpha-1) * N^(1-alpha);
util1=-10000*ones(nkap,nkap);
util2=-10000*ones(nkap,nkap);
for i=1:nkap;
kap=(i-1)*inckap;
for j=1:nkap;
kapp = (j-1)*inckap;
cons1 = wage + (rent + delta)*kap - kapp;
if cons1 > 0;
util1(j,i)=real((cons1)^(1-sigma)/(1-sigma));
end;
cons2 = theta*wage + (rent + delta)*kap - kapp;
if cons2 > 0;
util2(j,i)=real((cons2)^(1-sigma)/(1-sigma));
end;
end;
end;
v = zeros(nkap,2);
decis = zeros(nkap,2);
test = 10;
[rs,cs] = size(util1);
r1=zeros(size(util1(:,1),1),cs);
r2=zeros(size(util2(:,1),1),cs);
while test ~= 0;
for i=1:cs;
r1(:,i)=util1(:,i)+beta*(prob(1,1)*v(:,1)+ prob(1,2)*v(:,2));
r2(:,i)=util2(:,i)+beta*(prob(2,1)*v(:,1)+ prob(2,2)*v(:,2));
end;
[tv1,tdecis1]=max(r1);
[tv2,tdecis2]=max(r2);
tdecis=[tdecis1' tdecis2'];
tv=[tv1' tv2'];
test_mex = true;
test_mex = max(any(tdecis-decis));
v=tv;
decis=tdecis;
end;
decis=(decis-1)*inckap;
g2=zeros(cs,cs);
g1=zeros(cs,cs);
for i=1:cs
g1(i,tdecis1(i))=1;
g2(i,tdecis2(i))=1;
end
trans=[ prob(1,1)*g1 prob(1,2)*g1; prob(2,1)*g2 prob(2,2)*g2];
trans=trans';
probst = (1/(2*nkap))*ones(2*nkap,1);
test=1;
while test > 10^(-8);
probst1 = trans*probst;
test = max(abs(probst1-probst));
probst = probst1;
end;
kk=decis(:);
meanK=probst'*kk;
lambda=zeros(cs,2);
lambda(:)=probst;
[v1,d1]=eig(prob');
[dmax,imax]=max(diag(d1));
probst1=v1(:,imax);
ss=sum(probst1);
probst1=probst1/ss;
probk=sum(lambda');
probk=probk';
Kold = K;
Knew = g*meanK + (1-g)*Kold;
metric = abs((Kold-meanK)/Kold);
K = Knew;
liter = liter+1;
end;
grid = [ (0:inckap:maxkap)' ];
income = zeros(size(grid,1),2);
income = [real((rent*grid + wage)) real((rent*grid + wage*theta)) ];
[ pinc, index ] = sort(income(:));
summstat = pinc;
end

Answers (1)

Adam
Adam on 20 Apr 2015
I haven't really used Matlab Coder much myself though others at my work have to convert my code to C++.
At a glance it looks like an odd error because, irrespective of whatever else goes on in the function you assign a value to summstat on the 2nd line of your function.
I know Matlab Coder does often tend to optimise away certain code paths depending on an evaluation of the code usage and other stuff like that so maybe this is happening here, although I would still have thought the assignment of 0 at the start of the function would remain.
Have you tried commenting out all the code after the initial assignment to summstat and see if that rather degenerate function compiles with the Coder?
The only other thing I can se is that this line
coder.extrinsic('clock');
is the one thing that appears above that assignment. I have no idea what that does, but is it possible for it to cause the function to return immediately after executing that line and before the rest of the function? If so that would result in your error.

Categories

Find more on MATLAB Compiler in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!