calling a matlab function whose name contains a numeric variable

时间:2016-08-31 12:18:53

标签: matlab function-calls

I have a set of functions such that I want to apply each of them in a separate iteration. I label the functions as: Strategy1(x), Strategy2(x)....Strategy100(x). As you can see, there is a numeric variable in the name of the function. I want to achieve something like

LS = [Strategy1(x),Strategy2(x),...,Strategy100(x)];
Y = zeros(100,1);
for i = 1:1:100
    Y(i) = Strategyi(x);
end

I wonder if there is a way to achieve this goal in matlab?

1 个答案:

答案 0 :(得分:0)

您可以使用 str2func

创建函数句柄
n = 100;
Y = zeros(n,1);
for i = 1:n
    funcH =  str2func( sprintf('Strategy%d', i));
    Y(i) = funcH(x);
end

如果要在for循环外连接函数名,可以使用 srtcat

strcat('Strategy', strread( num2str(1:n), '%s'))