MATLAB Answers

sim_892
0

Variables with sequential names (with function handle @)

Asked by sim_892
on 22 Nov 2019 at 2:51
Latest activity Edited by Stephen Cobeldick on 22 Nov 2019 at 5:27
Hi, I am following the recommended way to create variables with sequential names: https://de.mathworks.com/help/matlab/matlab_prog/string-evaluation.html
However, each one of my variables in the cell array is a function with function handle, I am trying this:
numArrays = 10;
A = cell(numArrays,1);
for n = 1:numArrays
A {n} = @ {beta} 0.5 * (beta (n) + beta (n + 1));
end
For every cell of my cell array I would like to get: @(beta)0.5*(beta(1)+beta(2)), @(beta)0.5*(beta(2)+beta(3)), @(beta)0.5*(beta(3)+beta(4)).....
But instead I get this: @(beta)0.5*(beta(n)+beta(n+1)), @(beta)0.5*(beta(n)+beta(n+1)), @(beta)0.5*(beta(n)+beta(n+1)) ....
Thank you for your help.

  1 Comment

"I am following the recommended way to create variables with sequential names... each one of my variables in the cell array is a function with function handle"
Nothing in your description or code example bears any relation to creating "variables with sequential names", or indeed any relationship to the page that you linked to. You appear to be trying to define elements of a cell array, but that is quite unrelated to sequential variable names. Note that indexing or array elements are NOT synonymous with variable names: the contents of a cell array are not separate variables, they are arrays.
"I am trying this"
I very much doubt that, as the code you have shown contains a syntax error which results in this error:
In any case, whilst generating an array of function handles is certainly possible, it is not clear why you cannot just use one function with the appropriate input parameters (one of the main points of using functions is that they are "generic" to some extent and can be called for diffrerent data, it unlikely that for each diffferent set of meta-data a new function handle should be created).
However if in the unlikely event you really do need a cell array of almost-identical function handles, then once your code bug is corrected it does exactly what you requested. Lets try it on simple example data:
>> A{2}([1,2,3])
ans =
2.5
>> 0.5*(2+3) % "by hand"
ans =
2.5
What exactly is the problem?

Sign in to comment.

1 Answer

Answer by Steven Lord
on 22 Nov 2019 at 5:08

The anonymous function "remembers" the value n had when the anonymous function was created, so it will do what you want when it is called. You can check what the anonymous function "remembers" using the functions function. [As its documentation states, you should only use this for diagnostic and debugging purposes.]
To answer what I expect you will ask next:
  1. No, there is no way to have the anonymous function use the "remembered" value as part of its display.
  2. No, there is no way to change the "remembered" value of n without recreating the anonymous function.
But rather than create a bunch of anonymous functions that are all very similar, why not just create one that's more flexible?
A = @(beta, n) 0.5*(beta(n) + beta(n+1));
Or if you want a function that will process all of beta at once, look at movsum and/or movmean.

  0 Comments

Sign in to comment.