Borrar filtros
Borrar filtros

error using the cellfun function

2 visualizaciones (últimos 30 días)
Ariela Glikman
Ariela Glikman el 2 de Mzo. de 2019
Comentada: Star Strider el 2 de Mzo. de 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

Respuestas (1)

Star Strider
Star Strider el 2 de Mzo. de 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 comentarios
Ariela Glikman
Ariela Glikman el 2 de Mzo. de 2019
you are genius,
I really don't understand what did you write but its working. thankssד
Star Strider
Star Strider el 2 de Mzo. de 2019
My pleasure! Thank you.
If my Answer helped you solve your problem, please Accept it!

Iniciar sesión para comentar.

Categorías

Más información sobre MATLAB en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by