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.

 채택된 답변

Sean de Wolski
Sean de Wolski 2011년 5월 18일

0 개 추천

댓글 수: 4

Fangjun Jiang
Fangjun Jiang 2011년 5월 18일
Thank you for the info. I can see there are people FOR it and there are people AGAINST it. I am going to ask people to vote to gauge the interest.
Earl Glynn
Earl Glynn 2015년 7월 26일
It's four years later and Matlab still does not allow functions in a script.
I don't understand the point of Matlab markup when it's impossible to include a function as part of the script. Simple educational "howto" examples including functions using Markup are not possible.
I don't understand those saying scripting in Matlab is bad, when functions with dozens of arguments exist, which are as bad or worse, are nearly incomprehensible and difficult to debug.
A simple "driver" markup script to explore other MatLab code seems quite desirable. But I guess I should explore Jupyter as an alternative since it looks like functions will never be allowed in Matlab scripts. I don't understand why when many other languages allow function definitions almost anywhere.
Walter Roberson
Walter Roberson 2015년 7월 26일
Let me think: Fortran... C... C++... VBA... java 7 and earlier... Nope, none of those have functions as first class objects that can be defined almost anywhere.
Scripting is like leaving broken glass on the floor. If you know where it is and always remember to walk around it, it isn't going to be a problem.
Saying that functions with dozens of arguments are "as bad or worse" is like saying that if you provide people with pieces of metal and grindstones then that is worse than glass on the floor because some people might leave dangerous metal burrs on the things they shape.
I accidentally broke a bottle on my back walk yesterday. I swept it up, but I couldn't get at the slivers in some of the cracks, and it's pretty likely that my feet will discover a missed sliver at some point.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Bart
Bart 2016년 12월 19일

3 개 추천

I believe this is possible since version R2016b. See https://www.mathworks.com/help/matlab/matlab_prog/local-functions-in-scripts.html.

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

질문:

2011년 5월 18일

답변:

2016년 12월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by