Calling unknown function names
Show older comments
APSCalcset1 = str2func('HeatFluxCoolant_V1')
APSCalcset2 = str2func('HR_V1')
for funcrun = 1:100
if exist(['APSCalcset' num2str(funcrun)],'var')
processed = feval(eval(['APSCalcset' num2str(funcrun)]),tempdata);
end
end
So I know this is not ideal, attempting to use eval with you guys doesn't seem to go over well. I just can't figure out how to get the function handle in feval dynamically without it. The number of functions with the handle naming convention will be unknown.
3 Comments
Geoff Hayes
on 9 Nov 2020
Joel - so you have 100 functions stored within different variabls named APSCalcset1, APSCalcet2, etc.? Why not just create an array of function handles? For example, using the built-in functions, you could do
funcArray = {@sin;
@cos;
@tan};
for funcrun = 1:3
funcArray{funcrun}(pi/4)
end
Your code might look something more like
funcArray = {@HeatFluxCoolant_V1;
@HR_V1};
for funcrun = 1:2
processed = funcArray{funcrun}(tempdata);
% now save processed variable so that it doesn't get overwritten on next iteration
end
Joel Duncan
on 9 Nov 2020
"I don't know that I will have 100, it's just a placeholder for a possible number."
Just use one cell array, it can have any size... or even change size.
"The attempt here is to allow other users of the code to add their own call functions by following the consecutive number naming convention for their function handle variables."
Using a cell array would be simpler, more expandable, more efficient, easier to debug, etc.
"I can't seem to get it to work with the function handle variables."
Put them into a cell array. Call them. Geoff Hayes already showed you how.
Answers (0)
Categories
Find more on Performance and Memory in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!