Does anyone know how to loop through a list of dynamic functions?

Hello everyone,
I am trying to create a loop of functions, which basically loops through a list of functions; the reason is that I want to find the best training function for a particular neural network.
s.a = 'trainb';
s.b = 'trainbfg';
s.c = 'trainbfgc';
fn = fieldnames(s);
for n = 1:length(fn)
trainfn=['net.trainFcn=' s.(fn{n})];
trainfn;
I first set up a structure with the 3 different functions, trainb, trainbfg, and trainbfgc, then I hope to loop through the functions one by one so that I will pass 3 functions:
net.trainFcn = 'trainb';
net.trainFcn = 'trainbfg';
net.trainFcn = 'trainbfgc';
At the moment all this does is just prints the text and does not apply the functions, I have tried feval, eval having no success, any help would be greatly appreciated,

 Accepted Answer

I would recommend using function handles instead of quoted function names. There is also no reason to use a structure.
s = {@trainb, @trainbfg, @trainbfgc};
for n = 1:length(s)
net.trainFcn = s{n};
%now invoke the training here
end

More Answers (0)

Categories

Find more on Deep Learning Toolbox in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!