Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
having a problime with adding the function at the top of a code

Subject: having a problime with adding the function at the top of a code

From: Karbala'a Unvi. Science

Date: 16 Dec, 2013 18:25:07

Message: 1 of 2

hi everyone,
I am having a problem with adding the function at the top of any code that I write at my Matlab program, because it's keep saying the message (Error: Function definitions are not permitted in this context.) I need the help in that urgently.
Pleas need the help.
Ziyad

Subject: having a problime with adding the function at the top of a code

From: Steven Lord

Date: 16 Dec, 2013 18:38:16

Message: 2 of 2


"Karbala'a Unvi. Science " <ziyad.nabeel@gmail.com> wrote in message
news:l8ngi3$drk$1@newscl01ah.mathworks.com...
> hi everyone,
> I am having a problem with adding the function at the top of any code that
> I write at my Matlab program, because it's keep saying the message (Error:
> Function definitions are not permitted in this context.) I need the help
> in that urgently.
> Pleas need the help.
> Ziyad

You cannot define a function inside a script file.

http://www.mathworks.com/help/matlab/matlab_prog/create-functions-in-files.html

http://www.mathworks.com/help/matlab/matlab_prog/create-scripts.html

So something like this:

% begin myfile.m
x = 1:10;
y = x.^2;
mysubfunction(x, y)

function mysubfunction(in1, in2)
plot(in1, in2);
legend('Plot from mysubfunction')
% end myfile.m

is not allowed. Since the file does not begin with either of the keywords
FUNCTION or CLASSDEF, it is a script file and script files cannot contain
subfunctions. Compare this with:

% begin myfile.m
function myfile
x = 1:10;
y = x.^2;
mysubfunction(x, y)

function mysubfunction(in1, in2)
plot(in1, in2);
legend('Plot from mysubfunction')
% end myfile.m

which is allowed. Since it starts with the FUNCTION keyword, it is a
function file and function files are allowed to contain subfunctions.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us