2

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!

2

3 Answers 3

4

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

>> cubed = @(x)x^3;
>> cubed(2)
ans =
     8

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:

>> fact = @(val,branchFcns) val*branchFcns{(val <= 1)+1}(val-1,branchFcns);
>> returnOne = @(val,branchFcns) 1;
>> branchFcns = {fact returnOne};
>> fact(4,branchFcns)
ans =
    24
>> fact(5,branchFcns)
ans =
   120
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks! Can any function be defined in Matlab command line window? Why I couldn't?
@Tim: If you post your example, I can tell you why it didn't work. Also, see my edit.
@Tim: Glad to help. I have added an example for how to calculate factorials.
That code you posted from MATLAB Central looked very familiar, but it took me a few seconds until I realized I was the one who posted it! =D I guess a consistent variable naming scheme is a good thing for recognizing your own handy-work.
@gnovice: I didn't make the connection right away, but at least I recognize awesomeness when I see it :)
2

This isn't really a feature of a programming language but of an implementation of that programming language. For example, there exist C interpreters and Lisp Compilers. This is normaly called an REPL (Read-Eval-Print-Loop) and is generally a feature of interpreted implementations.

3 Comments

Thanks! Nice to know about that. Back to my original question, so is whether or not a definition of a function allowed to run in the command line window depends on the particular implementation? For the same language, different implementation's command line window may or may not allow the definition of a function in it?
@Tim, that's correct. If you are interested in a particular language, you just need to find an implementation with an REPL. Of course, there might or might not be one. Also, I'm pretty sure that you're talking about an REPL. "Command line window" is sort of non-standard terminology here but REPL is the only thing that seems to make sense here.
Thanks again. After reading wikipedia for REPL, I am now confused about what is the relation between REPL and interpreter?
1

Yes, if and when the language at hand supports it. So here is a trivial R example, cut and pasted from the command-prompt I am using:

R> cubed <- function(x) x^3
R> cubed(2)
[1] 8
R> cubed(3)
[1] 27
R> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.