best way to define a function inside a script

조회 수: 85 (최근 30일)
Alessio
Alessio 2011년 9월 14일
댓글: Steven Lord 2024년 4월 8일
Hi to everyone,
I'm currently searching the way to use functions inside a script, read documentation and so on..... I'm just an entry-level matlab user but i already spent too many hours searching in topics and documentations, without finding any answer about this. I think to have understood the difference between scripts and functions, but I'd like to know how can I define a function inside a script to use it several time into the script. I could define a function and save it as an m-file, and call it inside a script, but if my function has only few lines it's not so convenient... I would like to have only one m-file.
Many thanks in advance
Alessio
  댓글 수: 3
Pink_panther
Pink_panther 2024년 4월 7일
편집: Pink_panther 2024년 4월 7일
Miracles in 2024, now you can place function (ANYwhere !!) inside a script M-file. Just tested thiss in 2024a.
^%%%%%
Each local function must begin with its own function definition statement and end with the end keyword. The functions can appear in any order and can be defined anywhere in the script. Before R2024a: Local functions in scripts must be defined at the end of the file, after the last line of script code.
Mathworks
Steven Lord
Steven Lord 2024년 4월 8일
You can place functions almost anywhere in scripts as of release R2024a as stated in the Release Notes. You can't define them inside conditional statements.

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

채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 9월 14일
If the function is relatively small and easy, I think you can use anonymous function. Search for anonymous function in documentation for details. For example:
a=1;
b=2;
f=@(x) x/2;
c=f(a)
d=f(b)
Or inline function John mentioned above.
g=inline('x/2');
e=g(a)
  댓글 수: 4
Ahmed Hafez
Ahmed Hafez 2019년 3월 24일
I think you are right Nora
I already used it and it works
Thanks
Walter Roberson
Walter Roberson 2019년 3월 24일
R2016b added the possibility of functions inside scripts. The limitations are:
  1. The function name cannot be the same as the script name
  2. Each function must have a matching end line
  3. The functions must be after the script portion.

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

추가 답변 (4개)

Rodrigo Pereira
Rodrigo Pereira 2017년 11월 14일
I know this is old, but it has been viewed quite recently, so this answer may be useful.
Apparently you can do it since R2016b - perhaps Fangjun Jiang was able to wear them down? :)

John D'Errico
John D'Errico 2011년 9월 14일
The BEST way? There is NO way to define a function inside a script, unless you are talking about an anonymous function or an inline function.
It is best if you learn to avoid scripts anyway. Scripts lead you to bad habits in programming. They convince you to do things like clear at the beginning. Learn to use functions, where there is no need to clear your workspace when you start!!!
  댓글 수: 2
Alessio
Alessio 2011년 9월 14일
I would to accept your answer too but I realized it's not possible.. thank you too
Andrew Frane
Andrew Frane 2021년 3월 28일
편집: Andrew Frane 2021년 3월 28일
There's a time and place for scripts, and a time and place for functions.

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


Alessio
Alessio 2011년 9월 14일
Many thanks guys, you perfectly understood my problem and actually the anonymous function is what I was searching for.... but it seems to be usable only within the command line, not in the script, is it right? and about function_handle, is it the solution for using an aanonymous_function in the script? I'm really sorry for my confusion...
To D'Errico: are you meaning that it's better to use a function m-file as a main() function in C?
  댓글 수: 2
Fangjun Jiang
Fangjun Jiang 2011년 9월 14일
It can be used in the script. Save the code in my answer to a script and you can run it.
Sean de Wolski
Sean de Wolski 2011년 9월 14일
Yes. In theory, you never need scripts, you just have a driver function that either takes a few inputs or is hardwired to create what you need and return it.
Personally, however, I usually use a driver script with many cells in cell mode that calls a bunch of functions. I find it's easier to debug these.

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


Li Klein
Li Klein 2018년 10월 11일
Hey everyone... I came across this super old question while looking for something completely different. To me it sounds like Alessio was actually looking for something like this: Matlab-Function-Types
You'll find the descriptions of all the different MATLAB function types in the provided link.
I'll leave it here in case this question ever comes up for another (new) user.
  댓글 수: 1
John D'Errico
John D'Errico 2020년 3월 8일
MATLAB has evolved considerably since this question was asked. Yes, there are now more ways to define a function. And you can now indeed put functions inside scripts. But it is almost 10 years since the question was asked.

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by