MATLAB Answers

M-function in a M-script

105 views (last 30 days)
Fangjun Jiang
Fangjun Jiang on 18 May 2011
Answered: Bart on 19 Dec 2016
When I write M-scripts (It's a script, not an M-Function), sometimes I want to have a quick and dirty function, such as:
test1.m
a=1;
b=MyFunction(a)
Function y=MyFunction(x)
y=x/2;
end
But it is not allowed because "FUNCTION keyword is invalid heare". I have to create another M-file called MyFunction.m to define MyFunction.
Why is that? What is the rational behind it?
I know I can work this way:
test2.m
function test2
a=1;
b=MyFunction(a)
end
function y=MyFunction(x)
y=x/2;
end
Although no need to specify the argument for function test2, still, I have to add the first line for function test2, add an "end" for function test2. And worst of all, no easy access to base workspace data.

Accepted Answer

Sean de Wolski
Sean de Wolski on 18 May 2011
  4 Comments

Sign in to comment.

More Answers (1)

Community Treasure Hunt

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

Start Hunting!