How can I create multiple function handles in a for loop?

3 views (last 30 days)
Hello to everyone!
I am trying to create multiple function handles with a for loop. For example:
mymat = 1:10;
for t = 1:length(mymat)
fun_t = (@) x^2-mymat(t)*x+mymat(t);
end
I would like to have 10 function handles like:
fun_1 = (@) x^2-1*x+1
...
fun_10 = (@) x^2-10*x+10
How can I do this? Would it be more convenient to use a matrix or an array with all the functions together?
Thank you very much and have a nice weekend!
  2 Comments
Stephen23
Stephen23 on 7 Mar 2025
Edited: Stephen23 on 7 Mar 2025
"I would like to have 10 function handles like"
You might "like to have" them all named like that, but that would be very bad data design:
"Would it be more convenient to use a matrix or an array with all the functions together?"
Yes, it would be much more convenient. It would also be much more efficient, easier to debug, etc.
For example, use a cell array to store them:
But why are you creating lots and lots of separate functions like that, when you could create one that operates on the entire matrix at once? It seems like something that would be better implemented as one function.
What are you actually trying to achieve?
Carles
Carles on 10 Mar 2025
Edited: Carles on 10 Mar 2025
Hi Stephen. I have a histogram such as the one attached below, and I have done spline interpolation for the value of every bin at the center of it (in green).
I would like to know the area under the spline. If I have that F is the spline, then I can get the coefficients of the 3rd degree polynomials with F.coefs. However, the coefficients are defined as a matrix length(X_values)-1 x 4.
F = spline(X values, Y values);
polynoms = F.coefs;
My idea was to define length(X_values)-1 function handles to calculate afterwards the value of the integral in the whole domain, in that case:
fun_1 = @(x) polynoms(1,1)*x^3+polynoms(1,2)*x^2+polynoms(1,3)*x+polynoms(1,4)
...
fun_n = @(x) polynoms(n,1)*x^3+polynoms(n,2)*x^2+polynoms(n,3)*x+polynoms(n,4)
Area_1 = integral(fun_1, x_1, x_2)
...
Area_n = integral(fun_n, x_n, x_n+1)
sum(Area_1, ..., Area_n)
But I guess this is not the most appropiate approach. Any suggestions?

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 10 Mar 2025
Edited: Stephen23 on 10 Mar 2025
"I would like to know the area under the spline... But I guess this is not the most appropiate approach. Any suggestions?"
Use FNINT and FNVAL from the Curve-Fitting toolbox. I will assume that you have defined the end points of spline to be y=0 (otherwise you need to locate the x-axis intercepts using e.g. root finding... OR otherwise define the ends of your spline in a way that suits your problem).
y = [0,rand(1,5),0];
x = 1:numel(y);
pp = spline(x,y);
fnplt(pp)
fn = fnint(pp);
A0 = fnval(fn,x(end)) - fnval(fn,x(1))
A0 = 4.2455
If you do not have the Curve-Fitting toolbox, then here are some alternative approaches:
xx = linspace(x(1),x(end), 1e4);
yy = ppval(pp,xx);
At = trapz(xx,yy)
At = 4.2455
Ai = integral(@(x) ppval(pp,x), x(1),x(end))
Ai = 4.2455

More Answers (2)

Matt J
Matt J on 7 Mar 2025
Edited: Matt J on 7 Mar 2025
The best would be to just have a matrix-valued function,
fun=@(x) x.^2-mymat(:).*x+mymat(:)
but you could make a cell array of scalar functions as well,
for t = 1:length(mymat)
c=mymat(t);
fun{t} = @(x) x^2-c*x+c;
end

Diego Caro
Diego Caro on 10 Mar 2025
Store your function handles in a cell array.
my_mat = 1:10;
my_handles = cell(size(my_mat));
for t = 1:length(my_mat)
my_handles{t} = @(x) x.^2 - my_mat(t).*x + my_mat(t);
end
  1 Comment
Matt J
Matt J on 10 Mar 2025
Note that I already gave this as part of my answer 3 days ago. Also, it is not efficient to define the functions with the syntax,
@(x) x.^2 - my_mat(t).*x + my_mat(t);
because this causes the function handle to store the entire array my_mat which is not needed. It is for this reason that I extracted my_mat(t) to a separate variable,
c=mymat(t);
fun{t} = @(x) x^2-c*x+c;

Sign in to comment.

Categories

Find more on Spline Postprocessing in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!