MATLAB error message "This statement is not included in any function."

I am trying to define a simple function and then call it:

   function p=MyExp(N);
      p=[ 1 ]; % 0th order polynomial. 
      for k=1:N
         pk=1/(factorial(k));
         p=[pk,1];
      end
   end


   poly3=MyExp(3);
   disp (poly3)

      

MATLAB returns the message: Error: File: matlab_labIII_3_I.m Line: 10 Column: 1 This statement is not inside any function. (It follows the END, which ends the definition of the "MyExp" function.)

This script works well on OCTAVE!

thank

+3


source to share


1 answer


If you are using functions in Matlab script, you must have all the code inside functions (functions), of which there can be more than one. Similar products (Octave and Scilab) do not have this limitation.

There's a simple way out with minimal code change: wrap the non-functional code in a function and call that. The main function must first appear in the script.



function MyProgram()
   poly3=MyExp(3);
   disp (poly3)
end 

function p=MyExp(N);
      p=[ 1 ]; % 0th order polynomial. 
      for k=1:N
         pk=1/(factorial(k));
         p=[pk,1];
      end
end

      

Also, when you use functions, Matlab expects your file name to match the name of the function being called. So the file should be named MyProgram.m

(or whatever your main function is named).

+4


source







All Articles