Function Named with String Substitution

3 次查看(过去 30 天)
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 个评论
Steven Lord
Steven Lord 2022-4-19
Can you do this? Yes.
Should you do this? The general consensus is no. See that Answers post for an explanation and alternatives.
Thomas Kane
Thomas Kane 2022-4-19
Thanks for the link. I was looking for an expedient method. An array of functions F(1),F(2),F(3).... would also meet my needs and be better practice . I'll investigate this route.

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2022-4-19
编辑:Stephen23 2022-4-19
"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, ...
  1 个评论
Thomas Kane
Thomas Kane 2022-4-19
Perfect! This is the effect I wanted to achieve. I wasn't familiar with the cell function. Thank you for enlightening me to this indexing approach in MatLab.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by