Function Named with String Substitution
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I am creating individual interpolating functions for each of 10 sets (matrixes) of x,y,z,v data, and would like to name each function based on the index 'j' of the 'for' loop in which they are created. The desired result is a set of functions named F1,F2,F3,...,F10.
Is there a way in MatLab to use string substitution in the name creation of each function?
%--------------------------------------
for j=1:10
...
F%j% = griddedInterpolant(v%j%,'spline')
...
end
2 commentaires
Steven Lord
le 19 Avr 2022
Can you do this? Yes.
Should you do this? The general consensus is no. See that Answers post for an explanation and alternatives.
Réponse acceptée
Stephen23
le 19 Avr 2022
Modifié(e) : Stephen23
le 19 Avr 2022
"Is there a way in MatLab to use string substitution in the name creation of each function? "
Yes, but only if you want to force yourself into writing slow, complex, inefficient, obfuscated, buggy code that is hard to debug. It is an approach best avoided, and almost never required.
The simple and very efficient MATLAB approach is to use indexing. You should use indexing:
N = 10;
C = cell(1,N);
for k = 1:N
...
C{k} = griddedInterpolant(..,'spline');
...
end
Indexing is one of the very important basic concepts that is explained in the introductory tutorials:
You can use indexing with all array types: numeric, char, logical, cell, struct, table, ...
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Matrices and Arrays 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!