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

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

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

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)

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

you gave me an awesome answer on how to make the function! But, now that I've made, it, I can't figure out how to use it in the next piece of the code. The next section calls: [x1,x2,x3,x4,x5,x6,x7,x8,x9,]=loop5(@minfuntj,x1,x2,x3,x4,x5,x6,x7,x8,x9); and I want 'j' to be the index i just defined, so iteration 1 then class, minfunt1 which i made earlier in the loop, and minfun2 is called the second time together. It's probably a dumb question but I've been beating my head against a wall as to how to do the next step since I got my first answer.
How different are these functions? Totally different? Or are they very similar functions that you can just pass j into?
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.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 3 Jun 2014

Commented:

on 3 Jun 2014

Community Treasure Hunt

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

Start Hunting!