To make it clearer, the functions are generated from a script for some symbolic computations using matlabFuntion to generate said functions. This is done within a loop, with each iteration referring to some potential coefficient and potential function, this is why the generated functions are named (essentially as) functionn_k.
Calling functions that have a variable name
17 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have a set of functions that have the general naming scheme of: function%d_%d. They all have the same input and I want to call them within two loops (for example a loop for variable n and another for k, meaning calling for functionn_k). Is it possible to do so and how?
3 commentaires
Stephen23
le 29 Oct 2023
"...the generated functions are named (essentially as) functionn_k."
How exactly? Are you writing them as function files (Mfiles) or as variables in the workspace or something else?
Réponse acceptée
Matt J
le 27 Oct 2023
Modifié(e) : Matt J
le 27 Oct 2023
Just generate handles to all the functions. Then they can be used freely to make function calls, e.g.,
F=cellfun(@str2func, compose('function%d_%d',(1:3)',1:5), 'uni',0)
for i=2,
for j=3,
y=F{i,j}(5)
end
end
function y=function2_3(x)
y=10*x;
end
Plus de réponses (1)
Catalytic
le 27 Oct 2023
Déplacé(e) : Image Analyst
le 28 Oct 2023
That sounds like a bad idea. You should probably make a single function where (n,k) are one of the arguments -
function oneFunction(n,k,other_arguments)
end
0 commentaires
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!