Effacer les filtres
Effacer les filtres

choose multiple functions from a larger group

1 vue (au cours des 30 derniers jours)
simone zappalà
simone zappalà le 10 Juin 2022
Commenté : dpb le 12 Juin 2022
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 commentaires
dpb
dpb le 10 Juin 2022
Indexing expression into an array of function handles???
Walter Roberson
Walter Roberson le 10 Juin 2022
Yes, cell array of function handles. randperm(7, 4) to select random indices.

Connectez-vous pour commenter.

Réponses (3)

Image Analyst
Image Analyst le 10 Juin 2022
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 commentaires
simone zappalà
simone zappalà le 11 Juin 2022
i think with this i just take randomly four of the seven functions, i need to decides which one i want to choose.
Anyway thx for answer.
Image Analyst
Image Analyst le 11 Juin 2022
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

Connectez-vous pour commenter.


dpb
dpb le 11 Juin 2022
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...

Walter Roberson
Walter Roberson le 12 Juin 2022
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 commentaires
Image Analyst
Image Analyst le 12 Juin 2022
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
dpb le 12 Juin 2022
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".

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by