Is it possible to define a function in commandline window of Matlab? Looks no to me.
But for R, it is possible to do so. I was wondering why there is this difference and if there is more to say behind this kind of feature of a programming language, or can I say just interpretive language (such as Python, Bash,…)?
Thanks!
You can define functions in the command window of Matlab. It will be evaluated like a function, but it won’t be available to you in your next Matlab session (though you can save and load it like a variable).
As an example, I copy @Dirk Eddelbuettel‘s function
EDIT 1
Note that you can only define single-statement functions as anonymous functions in Matlab, so you can’t use e.g. for-loops (unless you use evil
eval, which allows everything). However, if you nest anonymous functions, you can create arbitrarily complicated recursive statements. Thus, I guess that you can indeed define any function in the command line window. It might just not be worth the effort, and I bet that it’ll be very difficult to understand.EDIT 2 Here’s an example of a recursive nested anonymous function to calculate factorials from Matlab central: