MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by Andres
on 14 Mar 2013

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.

*No products are associated with this question.*

Answer by Andres
on 15 Mar 2013

Accepted answer

I solved this with a for statement. I.e.,

[rows, cols]=size(ab); for (i=1:rows) if (ab(i,1)==1) sync(i,1:4)=hex{i,1}(1:4); comm(i,1:4)=hex{i,1}(5:8); end

if (ab(i,1)==0) sync(i,1:4)=hex{i,1}(1:4); comm(i,1:4)=hex{i,1}(5:8); end end

Answer by Jan Simon
on 14 Mar 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

Andres
on 14 Mar 2013

Thank you Jan Simon for the response. Pardon the clarity of my problem. Your solution without cellfun does not work correctly for what I need. Since the first value in the cell ab is 1, it is going to define sync1 and comm1 for all my data assuming ab is always 1. This is not the case because ab is also 0 in certain rows.

To explain better, I have a .txt file in which I imported to Matlab using textscan. This text file has 4 columns of data which are 1. Channel, 2. Date, 3. Time, 4. Message data. The specific problem I am asking here is for 4. Message data. The imported data becomes a 90x1 cell for Message data. The data looks similar to:

FFFF0675000000045234

FFFF0541000000056201

FFFF064C00270070203430123054

...

Well this cell is 90x1 where the data varies from 20 characters to 28 characters. I am now trying to separate cell 1, cell 2, cell 3 etc. based on length of the cell (20 or 28). Thus I defined a function that says,

gh==cellfun('length',hex)==20; %note hex is the name of the 90x1 cell ab==num2cell(gh);

So ab becomes 1 if the cell is 20 characters and 0 if the cell is 28 characters. After this, I am creating the if statement to tell matlab how to break up either a 20 character message or 28 character message so I can than analyze this data correctly.

So I was thinking of using cellfun again so I can check the value in each cell and than based on the value in the cell (0 or 1), break up the message data appropriately (sync1, comm1, sync2, comm2 etc.).

Please let me know if this clarifies my problem.

Thank you

## 2 Comments

## Andrei Bobrov

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/67120#comment_136395

what is it

hex## Andres

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/67120#comment_136540

Hello Andrei, the message data "hex" is a combination of hex and binary numbers. An example of the "hex" cell array is:

FFFF0675000000045234

FFFF0541000000056201

FFFF064C00270070203430123054

...