3

Is there a good reason to choose between using inline functions vs anonymous functions in MATLAB? This exact question has been asked and answered here, but the answer is not helpful for rookie MATLAB users because the code snippets are incomplete so they do not run when pasted into the MATLAB command window. Can someone please provide an answer with code snippets that can be pasted into MATLAB?

Chad
  • 1,434
  • 1
  • 15
  • 30
  • Perhaps this contains what you are looking for: http://www.mathworks.de/matlabcentral/answers/7860 – Dennis Jaheruddin Aug 15 '13 at 13:04
  • 1
    *"but the answer is not helpful because the code snippets are incomplete so they do not run when pasted into the MATLAB command window"* This in not true. Read the answer carefully. – Oleg Aug 15 '13 at 13:10
  • No, that is exactly the Q&A that I posted in the link in my question. I cannot run the incomplete code snippets from Oleg's answer, and without being able to run the code, I do not understand the answer. – Chad Aug 15 '13 at 13:11
  • @OlegKomarov, you do not define x. Hence, I can't simply copy and paste your answer to see the point. You're putting a lot of onus on your readers to figure out what you're trying to show, and this could be eliminted with a very small change in your answer. – Chad Aug 15 '13 at 13:16
  • Also, the function defined in Step 1 is not used until the variable `a` has been redefined in step 2 so it is not clear what happens when I change `a` after defining an anonymous function. – Chad Aug 15 '13 at 13:17
  • 2
    @Chad In Oleg's step 1, `x` is defined by the anonymous function as it's input parameter. So that is 100% complete. As for step 2, `x` can be anything. Using `x = 2` is probably simplest. I believe the point is to compare `y(2)` before and after setting `a=3`. Give it a try. – Dan Aug 15 '13 at 13:21
  • Short answer: don't use inline functions unless you're using a truly ancient version of Matlab that doesn't support anonymous functions. – horchler Aug 15 '13 at 14:18

2 Answers2

8

Anonymous functions replaced inline functions (as mentioned in both the docs and in the link you posted)

The docs warn:

inline will be removed in a future release. Use Anonymous Functions instead.

Dan
  • 45,079
  • 17
  • 88
  • 157
  • Excellent. This is a constructive answer. My version of MATLAB is sufficiently old that this was not in the documentation, but it makes sense. – Chad Aug 15 '13 at 13:14
  • 1
    @Chad This is also in the link you posted, as a comment on Oleg's answer. – Dan Aug 15 '13 at 13:19
  • I guess I missed that in my frustration in trying to figure out what the code snippets were doing. – Chad Aug 15 '13 at 13:20
2

Here is how I would present Oleg's answer in my own style:

Case 1 - define anonymous function with parameter a and argument xin

a = 1;
y = @(x) x.^a;
xin = 5;
y(xin) 
% ans =
%      5

Case 2 - change parameter a in the workspace to show that the anonymous function uses the original value of a

a = 3;
y(xin)
% ans =
%      5

Case 3 - both inline and anonymous functions cannot be used if they contain parameters that were undefined at the time of definition

clear all
y = @(x) x.^a;
xin = 5;
y(xin)
% ??? Undefined function or variable 'a'.

% Error in ==> @(x)x.^a

z = inline('x.^a','x');
z(xin)
% ??? Error using ==> inlineeval at 15
% Error in inline expression ==> x.^a
% ??? Error using ==> eval
% Undefined function or variable 'a'.
% 
% Error in ==> inline.subsref at 27
%     INLINE_OUT_ = inlineeval(INLINE_INPUTS_, INLINE_OBJ_.inputExpr, INLINE_OBJ_.expr);

Case 4 Comparing performance and passing a as a variable.

clear all;
y = @(x,a) x.^a;
ain = 2;
xin = 5;
tic, y(xin, ain), toc
% ans =
%     25
% Elapsed time is 0.000089 seconds.

tic, z = inline('x.^a','x','a'), toc
z(xin, ain)
% z =
%      Inline function:
%      z(x,a) = x.^a
% Elapsed time is 0.007697 seconds.
% ans =
%     25

In terms of performance, anonymous >> inline.

Chad
  • 1,434
  • 1
  • 15
  • 30
  • 2
    If you really want to be super clear, then don't use `x` in both the anonymous function and the main code. The `x` in the function is a dummy variable anyway. Plus you should define your `x=5` after your anonymous function. But personally I found Oleg's answer pretty clear to be dead honest. – Dan Aug 15 '13 at 13:42
  • You're **completely** missing the point of anonymous functions. Please read the documentation before introducing confusion by 'cleaning' my answer. Additionally, the `clear all` at the beginning of the code, for instructing purposes is totally misused and redundant. – Oleg Aug 15 '13 at 16:21
  • I have edited it to show that parameters must be defined prior to defining the function. It also shows that parameters in the function are not tied to the base workspace. Lastly, it shows that argument variables can be passed the to function after defining the function. I have also accomplished my goal of posting an answer that will run in the MATLAB command window. – Chad Aug 15 '13 at 20:11
  • I have fully implemented all feedback on this post so I would appreciate it if whoever voted it down would undo the down vote, provide more feedback, or edit the post yourself. – Chad Aug 15 '13 at 20:17
  • 2
    I appreciate how you elaborated on my answer, which by personal style is succinct to the bone (and are usually tailored to the inferred knowledge of the poster). I will be happy to reverse the downvote if you amend the "clean" with "elaborated on", "expanded on" or similar. – Oleg Aug 15 '13 at 21:01
  • @Oleg, done. It is good to have the occasional necessary reminder that I should put effort into being diplomatic. – Chad Aug 16 '13 at 22:05
  • 1
    Btw I totally agree that you improved on my answer I just did not see how there would be anything to clean since it was already minimal. – Oleg Aug 17 '13 at 11:14