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!