Give an unknown number of parameters to feval

6 次查看(过去 30 天)
Hello,
I have a lot of signals on which I want to compute differents segmentation methods (in order to compare the differents methods). For each method, I created a function. Then, to apply each method to all of my signals I decided to store all the handles of the functions I want to apply and use feval in a loop :
(All of my signals have the same length so they are saved in the columns of a matrix named 'signalsMatrix')
numberOfChangePointsToFind = 4;
numberOfSignals = size(signalsMatrix,2);
segmentationFunctionsHandles = {@function1,@function2,@function3,...};
segmentationResults = struct('segmentationMethod',{},'changePointsIndexes',{});
for methodNumber = 1:numel(segmentationFunctionsHandles)
segmentationResults(methodNumber).changePointsIndexes = zeros(numberOfChangePointsToFind,numberOfSignals);
for signalNumber = 1:numberOfSignals
y = signalsMatix(:,signalNumber);
segmentationResults(methodNumber).changePointsIndexes(:,signalNumber) = feval(segmentationFunctionsHandles{methodNumber},y,numberOfChangePointsToFind);
end
end
At first it worked fine because all my functions took the same number of parameters : 'y' and 'numberOfChangePointsToFind' .
But now I have new segmentation methods that have more parameters. For instance 'y' , 'numberOfChangePointsToFind' and 'kernelBandwidth' . So is there a way to give to feval parameters that have been previously saved in a variable ?
Like that for instance :
numberOfChangePointsToFind = 4;
numberOfSignals = size(signalsMatrix,2);
segmentationFunctionsHandles = {@function1,@function2,@function3,...};
segmentationParameters = {[],[],[3],...};
segmentationResults = struct('segmentationMethod',{},'changePointsIndexes',{});
for methodNumber = 1:numel(segmentationFunctionsHandles)
segmentationResults(methodNumber).changePointsIndexes = zeros(numberOfChangePointsToFind,numberOfSignals);
for signalNumber = 1:numberOfSignals
y = signalsMatix(:,signalNumber);
parameters = [[y,numberOfChangePointsToFind], segmentationParameters{signalNumber}];
segmentationResults(methodNumber).changePointsIndexes(:,signalNumber) = feval(segmentationFunctionsHandles{methodNumber},parameters);
end
end
Thank you

采纳的回答

Steve Eddins
Steve Eddins 2020-12-7
Put your parameters in a cell array and then use the syntax that expands a cell array into a comma-separated list:
out = feval(f,parameters{:});

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programmatic Model Editing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by