How to call a different function for each iteration of a loop?

7 views (last 30 days)
If I have created a set of functions with names corresponding to indices, How do I later call that function with the index I am on? e.g. in a "for" loop, call "minfunt1" on iteration 1, "minfunt2" on iteration 2, etc.?

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 3 Jun 2014
funcells = cell(15,1);
for jj=1:15
Minsum=minimize6(D0,D1,D2,D3,D4,D5,D6,D7,D8,Data104trans(jj,3:13));
a = sprintf('minfunt%d',jj);
matlabFunction(Minsum,'file',a);
funcells{jj} = str2func(a);
end
use your functions in loop
for jj = 1:15
[x1,x2,x3,x4,x5,x6,x7,x8,x9,]=loop5(funcells{jj},x1,x2,x3,x4,x5,x6,x7,x8,x9);
end
  1 Comment
Darya
Darya on 3 Jun 2014
This returns me this error. Error using sym>notimplemented (line 2682) Function 'le' is not implemented for MuPAD symbolic objects.
Error in sym/le (line 834) notimplemented('le');
Error in fminbnd (line 308) if fu <= fx
Error in loop5 (line 4) [x1,fval]=fminbnd(@(x1) fun(x1,x2,x3,x4,x5,x6,x7,x8,x9),0,1);
Error in varmanytestCleanUp2 (line 19) [x1,x2,x3,x4,x5,x6,x7,x8,x9,]=loop5(funcells{jj},x1,x2,x3,x4,x5,x6,x7,x8,x9);

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 3 Jun 2014
I don't understand how this is different than your prior answer in http://www.mathworks.com/matlabcentral/answers/132127#answer_139195 which I answered and you accepted.
  3 Comments
Image Analyst
Image Analyst on 3 Jun 2014
How different are these functions? Totally different? Or are they very similar functions that you can just pass j into?
Darya
Darya on 3 Jun 2014
I don't really understand the question. :( each function J is made from a slight modification of the base function, and then saved like you showed me before. I can't use the %d thing though because when i try to put it in loop5(@minfunt%d matlab squawks at me.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!