choose multiple functions from a larger group
1 次查看(过去 30 天)
显示 更早的评论
I've 7 function, i want to be able to choose several of them, I know with if and swich i can pick only one of these seven, i need something that allows me to select, for example, 4 function of these functions from the main group.
2 个评论
Walter Roberson
2022-6-10
Yes, cell array of function handles. randperm(7, 4) to select random indices.
回答(3 个)
Image Analyst
2022-6-10
How about this:
numFunctionsToCall = 4;
randomNumbers = randi(7, numFunctionsToCall, 1)
for k = 1 : numFunctionsToCall
thisNumber = randomNumbers(k);
if thisNumber == 1
results = function1();
elseif thisNumber == 2
results = function2();
elseif thisNumber == 3
results = function3();
elseif thisNumber == 4
results = function4();
elseif thisNumber == 5
results = function5();
elseif thisNumber == 6
results = function6();
elseif thisNumber == 7
results = function7();
end
end
If not, then explain better how you want it to operate.
2 个评论
Image Analyst
2022-6-11
Then just assign the ones you want to use, or ask the user for them with input() or some drop down lists.
functionsToCall = [6,3,4,1]; % Define which functions are to be called, in the order they are to be called.
for k = 1 : length(functionsToCall)
thisNumber = functionsToCall(k);
if thisNumber == 1
results = function1();
elseif thisNumber == 2
results = function2();
elseif thisNumber == 3
results = function3();
elseif thisNumber == 4
results = function4();
elseif thisNumber == 5
results = function5();
elseif thisNumber == 6
results = function6();
elseif thisNumber == 7
results = function7();
end
end
dpb
2022-6-11
Using listdlg would be one easy-to-code way to get started -- although it doesn't have the facility to limit the number selected in 'MultiSelect' mode.
You could put it in a loop and repeat with the list not containing the already selected values until four were selected with single selection only.
Or you could throw away more than four if user got overly exuberant in multiple selections -- of course, then it's again somewhat arbitrary about which one you keep/don't.
If we had any idea of what the end purpose here were to be, might be easier to make more specific recommendations...
0 个评论
Walter Roberson
2022-6-12
functions_to_pick_from = {@first, @second, @third, @fourth, @fifth, @sixth, @seventh};
num_to_pick = 4;
num_to_pick_from = length(functions_to_pick_from);
random_indices = randperm(num_to_pick_from, num_to_pick);
random_handles = functions_to_pick_from(random_indices);
for K = 1 : num_to_pick
results{K} = random_handles{K}(x); %run the function on appropriate input
end
2 个评论
Image Analyst
2022-6-12
She didn't want random: "i think with this i just take randomly four of the seven functions, i need to decides which one i want to choose." So maybe, or maybe not, she has a way to decide on the 4 functions to choose from, and their order, like an edit field on a GUI or a call to the input function.
dpb
2022-6-12
But since she didn't provide any klews as to what, specifically, she did/does want, I think Walter just used randperm to illustrate picking a set of four function handles from the function handle array and how to use those -- it's up to OP to decide the "how".
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!