How to deal with error "Input #3 expected to be a cell array, was double instead." using cellfun?
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello everyone,
as I'm trying to lean how to code with vectorization and do this magic with little lines and incredible performence there are a few hurdles for me to overcome. First I need to learn how to use cellfun correctly. Though I think the descriptions MathWorks gives are often usefull they are just examples and not always show me the solutions to my issues. Please note I am here to learn. There are a lot of things to learn for me. So I am thankful for ever advice to improve my coding. And I am not a native english speaker so there is the possibility that I just don't get right what the discription is supposed to tell me.
I have the following code:
[~,locs{1},~,~] = findpeaks(Mbx,t,'MinPeakHeight',maxPeakx,'MinPeakWidth',MinPeakWi,'MinPeakDistance', MinPeakDis ,'Annotate','extents');
[~,locs{2},~,~] = findpeaks(-Mbx,t,'MinPeakHeight',maxPeakx,'MinPeakWidth',MinPeakWi,'MinPeakDistance', MinPeakDis,'Annotate','extents');
[~,locs{3},~,~] = findpeaks(Mby,t,'MinPeakHeight',maxPeaky,'MinPeakWidth',MinPeakWi,'MinPeakDistance', MinPeakDis ,'Annotate','extents');
[~,locs{4},~,~] = findpeaks(-Mby,t,'MinPeakHeight',maxPeaky,'MinPeakWidth',MinPeakWi,'MinPeakDistance', MinPeakDis,'Annotate','extents');
In this case Mbx, Mby and t are arrays and the code works. It gives me the locations of the peaks in terms of t (time vector).
But I want to be able to get the same result in just one line without using a loop.
So I formed a cellarray with:
Mb = {Mbx -Mbx Mby -Mby};
and tried to perform a cellfun like this:
[~,locs,~,~] = cellfun(@(x)findpeaks(x,t,'MinPeakHeight',maxPeakx,'MinPeakWidth',MinPeakWi,'MinPeakDistance', MinPeakDis ,'Annotate','extents'),Mb,'UniformOutput',false);
I thought x is the input for my function and it performes findpeaks with all given name and value pairs on each cell of it.
But a get the error:
Error using cellfun
Input #3 expected to be a cell array, was double instead.
So what is Input #3 in this case? And how do I hand over the name value pairs for findpeaks correctly to the cellfun?
kind regards
Fabian
0 Kommentare
Antworten (1)
David Hill
am 25 Jun. 2021
Loops are not bad. cellfun is performing a loop.
Mb = {Mbx -Mbx Mby -Mby};
for k=1:4
[~,locs{k},~,~] = findpeaks(Mb{k},t,'MinPeakHeight',maxPeakx,'MinPeakWidth',MinPeakWi,'MinPeakDistance', MinPeakDis ,'Annotate','extents');
end
5 Kommentare
Stephen23
am 25 Jun. 2021
Bearbeitet: Stephen23
am 25 Jun. 2021
"I read comments from which I conclude the opposite"
Please give a link to that MATLAB Answers thread.
"You are of the oppinion I should concentrade on writing better loops then?"
No, I am of the opinion that every tool has its uses.
"as I'm trying to lean how to code with vectorization and do this magic with little lines and incredible performence there are a few hurdles for me to overcome. First I need to learn how to use cellfun correctly."
CELLFUN is not really relevant to code vectorization. Read the definition here:
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!