MATLAB Answers

Give an unknown number of parameters to feval

20 views (last 30 days)
anna444
anna444 on 7 Dec 2020
Commented: anna444 on 7 Dec 2020
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

  0 Comments

Sign in to comment.

Accepted Answer

Steve Eddins
Steve Eddins on 7 Dec 2020
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{:});

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!