How do I use cellfun to check numbers in a nx1 cell for an if statement?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I have a cell array which contains values 0 or 1. I want to look into this cell array and create an if statement that will allow me to distinguish between 2 types of messages. The cell array is similar to the following:
1
1
1
0
1
0
0
0
1
...
Where it is a 90x1 cell. The problem I am having is creating a if statement to read each cell and than categorize the cell as appropriate.
Currently I am trying the following (Where ab is my 90x1 cell):
if cellfun(@double,ab)==1
sync1 =cellfun(@(x) x(1:4),hex,'UniformOutput', false); %break up message
comm1=cellfun(@(x) x(5:8),hex,'UniformOutput', false);
...
elseif cellfun(@double,ab)==0
sync2 =cellfun(@(x) x(1:4),hex,'UniformOutput', false);
comm2=cellfun(@(x) x(5:8),hex,'UniformOutput', false);
...
end
The "..." is where I categorize this data based on whether it is a "1" or "0". I am not getting my if statement to work for each value in the cell array. I do not have errors but none of my scripts following the if and ifelse statement are executing so I know I am not writing my cellfun correct.
Any help is greatly appreciated.
Thank you.
2 comentarios
Respuesta aceptada
Más respuestas (1)
Jan
el 14 de Mzo. de 2013
When ab is your cell, cellfun(@double, ab) converts its elements to a double vector. Then if cellfun(@double,ab)==1 is equivalent to:
d = [ab{:}];
if d==1
When if gets a vector as condition, it performs this implicitly:
if all(d==1) & ~isempty(d)
I do not think, that this is your intention. But currently it is not clear, what your program should achieve. I believe, you do not need cellfun at all, but this would be much easier:
if ab{1} == 1
sync1 = x(1:4);
comm1 = x(5:8);
else % No condition required, if the input can be 0 or 1 only
sync2 = x(1:4);
comm2 = x(5:8);
end
Ver también
Categorías
Más información sobre Cell Arrays en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!