error using the cellfun function

2 views (last 30 days)
Ariela Glikman
Ariela Glikman on 2 Mar 2019
Commented: Star Strider on 2 Mar 2019
Hi guys,I have a cell with names (first.last) and I wrote a function who receives names and checking if they are written correctly.
I'm trying to call the function but there is an error.
function [studBonus] =kahootRes
name= {'Harry.Potter', 'Albu.Dom', 'vold lors'};
ansVec= [1 2 3 3 5 6 0 8 6 6], [1 2 0 4 5 6 0 8 0 6], [1 2 3 4 5 6 0 8 9 6];
fileName= struct('name', name, 'ansVec', ansVec);
fullName= {fileName.name};
[resValid, firstName, lastName]= cellfun(@checkName,fullName); % here is the error
this is the function I wrote, it not really important for you but I add it anyway.
function [resValid, firstName, lastName] = checkName(fullName)
posDot= find(fullName=='.');
countDot = length(posDot);
resValid=0;
firstName=[];
lastName=[];
if (countDot==1)
resValid= 1;
firstName= fullName(1:posDot-1);
lastName= fullName(posDot+1:end);
elseif ((countDot==0)|| (countDot>1))
resValid= 0;
firstName= fullName;
lastName=[];
end

Answers (1)

Star Strider
Star Strider on 2 Mar 2019
The error I get from the code as you posted it is:
Error using cellfun
Non-scalar in Uniform output, at index 1, output 2.
Set 'UniformOutput' to false.
So, do exactly that, and the error no longer exists:
[resValid, firstName, lastName]= cellfun(@checkName,fullName, 'UniformOutput',false)
or more commonly:
[resValid, firstName, lastName]= cellfun(@checkName,fullName, 'Uni',0)
  2 Comments
Ariela Glikman
Ariela Glikman on 2 Mar 2019
you are genius,
I really don't understand what did you write but its working. thankssד
Star Strider
Star Strider on 2 Mar 2019
My pleasure! Thank you.
If my Answer helped you solve your problem, please Accept it!

Sign in to comment.

Products


Release

R2016a

Community Treasure Hunt

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

Start Hunting!