Output argument not assigned on some execution paths

I am trying to run MATLAB Coder to convert a MATLAB.m file to .mex in order to hopefully run the code faster. When trying to create a file using MATLAB Coder engine, I get the following error:

Output argument 'summstat' is not assigned on some execution paths

      

Where summstat

is the funtion output argument that I am trying to compile. On the last line of my code, I assign summstat

:

summstat = pinc;

      

where pinc

is another variable used throughout the code. Most of this type of problem occurs when an output variable is assigned within a branch if

, but this is not the case here. Although there are if

u in all the code for

, the assignment is not in any of them (as I said, this is the last line of the function).

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

      

+3


source to share


2 answers


This is a case of a failed message. In short, the problem is that the loop while

with the condition:

while test ~= 0;

      

is infinite and can be defined as infinite when generating code. Note that it is test

set to 10 before the cycle and is test

never written during the cycle.

Thus, in fact, the output will never be written, because the control will never reach that loop. To test this theory, I added:

test = test - 1;

      



inside this loop while

and was able to generate the code with:

codegen fun -args {1:4,1} -report

      

I have communicated this to the MATLAB Coder team, so we may consider improving this case in the future.

For completeness, if an infinite loop is really needed (for a server loop, for example), then the containing function can be written so as not to have an exit, since that function will never return.

+2


source


My first thought would be hidden break

or while

that seems to go on forever.

This suggests that I cannot define them in your code, so this is how you might approach the problem.



  • The problem goes away if you remove the part summstat = pinc

    ?
  • Or if you change it to summstat = 0

    ?

Otherwise, I recommend that you start cutting parts of the code until the error goes away and you can increase some suspicious lines. Perhaps you can start by cutting out each of the EXCEPTs summstat = 0

on the first line and see if that works as expected.

0


source







All Articles