I have found anonymous function pretty useful, but a lot of times I'll need to use a loop in order to make the function work. For example:
while val<tolerance
.....
end
I am aware that I can save the function in a separate file, and that sometimes I can vectorize the code and then the anonymous function can work, but in some cases it is very hard to find an alternative to a for loop.
Matlab documentation doesn't discuss it or say it is impossible. Any ideas?
The Functional Programming constructs on the Mathworks file exchange are precisely what you need. Each of these functions are designed to be used within anonymous functions. They are discussed in detail in a 3 part series on the Loren on the Art of MATLAB Blog: Part 1, Part 2 and Part 3.
In particular Part 3 discusses implementing loops as a function. For completeness, I will borrow some of the code from Functional Programming FEX submission to demonstrate how in m-code we can use a while loop within an anonymous function. Firstly, define a loop
function:
function x = loop(x, continueFcn, f)
% Inputs:
% x - Initial state (can be cell array of arguments to f)
% continueFcn - Continue function, returns true iff the loop should go on
% f - Function of the state (x) to run every iteration
while ~continueFcn(x{:})
x = f(x{:});
end
end
For the example provide val
wile have some initial value, val0
say. Further, suppose that StuffDoneEachWhileLoop
is a function that defines how the variable val
should update in each while loop. Then:
myFunc = @(n) loop(val0, ... % Initialize state
@(val) val < tolerance, ... % OP condition
@(val) StuffDoneEachWhileLoop(val)); %
Various extensions to the above idea are possible. See Tucker McClure's Functional Programming FEX submission for further details.
If your main bother is having to create a new m file each time you want to use a simple function, this could be a simple solution for you.
If you are writing a function, and want to use another function in only this scope you do not need to create a new m file. Instead you can just define a second function which is available locally.
Check this for example:
function [m,s] = stat2(x)
n = length(x);
m = avg(x,n);
s = sqrt(sum((x-m).^2/n));
end
function m = avg(x,n)
m = sum(x)/n;
end
http://www.mathworks.nl/help/matlab/ref/function.html;jsessionid=903a612c197e288e7cf631d7a434
Matlab's documentation actually does discuss it, however the explanation might be considered somewhat vague. From the anonymous function documentation:
Anonymous functions can accept inputs and return outputs, just as
standard functions do. However, they can contain only a
single executable statement.
What I understand under "executable statement" is actually an expression that obtains some value.
As far as I know, loops in Matlab do not have a value and therefore cannot be used as the single executable statement inside an anonymous function. Furthermore, the constraint of just a single statement means that you cannot use the loop for some computation and then return some value with a second statement inside the anonymous function.