MATLAB Answers

0

Concatenate Columns of Cells

Asked by Chameleon17 on 2 Jun 2015
Latest activity Commented on by Jan Orwat
on 2 Jun 2015
Hi,
I have asked this question earlier, but I think I have though of how to better phrase it.
Columns 1 through 5
'01/04/2004' [ 0] [ 0] [ 0] [ 0]
'01/04/2004' [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] '05/04/2004'
[ 0] [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] '04/04/2004' [ 0]
[ 0] [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] '04/04/2004' [ 0]
[ 0] '02/04/2004' [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] '05/04/2004'
[ 0] [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] '05/04/2004'
[ 0] [ 0] [ 0] '04/04/2004' [ 0]
[ 0] [ 0] [ 0] '04/04/2004' [ 0]
[ 0] [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] [ 0]
'01/04/2004' [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] '03/04/2004' [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] [ 0]
'01/04/2004' [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] '03/04/2004' [ 0] [ 0]
[ 0] [ 0] '03/04/2004' [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] '05/04/2004'
There is only ever one date per row, and I want to concatenate them into one column, removing all the zeros.
I think it is difficult because of the cell/date format that I have going on. I've managed to make one very long vector of it all, and get rid of the zeros but lose the order they are in.
Any thoughts would be very much appreciated! Or any reading, or hints even...
Thanks!

3 Answers

Answer by Jan Orwat
on 2 Jun 2015

Hello, I can see that date is of type string in your cell array and non-date fields are zeros (non-string). Assuming that in every row there is just one date you can get those like that:
c = yourCell.'; % rows becomes columns, columns becomes rows - this is for MATLAB indexing purposes
datesOnly = c(cellfun(@isstr,c));
This method will delete those rows which doesn't contain dates at all. It won't work properly if row contain more than one date because it gets all dates from array row by row.

  2 Comments

Hi!
Thank you a lot! This worked! I've been stuck here for so long.
I have come up with a different problem now. I have realized that not every row has a date... but I need them to maintain their original order, with a space if the date is missing. Do you know if it would be possible to do this?
Jan Orwat
on 2 Jun 2015
Hope this will help:
>> a = {
[ 0] [ 0] '20/7/2014'
[ 0] '23/7/2014' [ 0]
'26/7/2014' [ 0] [ 0]
[ 0] [ 0] [ 0]
'21/7/2014' [ 0] [ 0]};
>> [r,c] = find(cellfun(@isstr,a));
>> dates = accumarray(r,r+(c-1)*size(a,1),[],@(x)a(x),{'missing'})
dates =
'20/7/2014'
'23/7/2014'
'26/7/2014'
'missing'
'21/7/2014'
>> % or:
>> dates = accumarray(r,r+(c-1)*size(a,1),[],@(x)a(x),{0})
dates =
'20/7/2014'
'23/7/2014'
'26/7/2014'
[0]
'21/7/2014'

Sign in to comment.


Answer by Thomas Koelen on 2 Jun 2015

I think this does what you want :)
a={'20/7/2014' 0 0; 0 '23/7/2014' 0; 0 0 '26/7/2014'};
j=1;
for i=1:size(a,1)*size(a,2)
if a{i}~=0
x(j)=i;
j=j+1;
end
end
for k=1:j-1
anew{k,1}=a{x(k)};
end
anew
a =
'20/7/2014' [ 0] [ 0]
[ 0] '23/7/2014' [ 0]
[ 0] [ 0] '26/7/2014'
anew =
'20/7/2014'
'23/7/2014'
'26/7/2014'

  1 Comments

Jan Orwat
on 2 Jun 2015
Hi Thomas, in your example, when you change a:
>> a={0 0 '20/7/2014'; 0 '23/7/2014' 0; '26/7/2014' 0 0}
a =
[ 0] [ 0] '20/7/2014'
[ 0] '23/7/2014' [ 0]
'26/7/2014' [ 0] [ 0]
it gives:
>> anew
anew =
'26/7/2014'
'23/7/2014'
'20/7/2014'
so it looks like it does not work.

Sign in to comment.


Answer by Andrei Bobrov
on 2 Jun 2015

d = { '01/04/2004' [ 0] [ 0] [ 0] [ 0]
'01/04/2004' [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] '05/04/2004'
[ 0] [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] '04/04/2004' [ 0]
[ 0] [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] '04/04/2004' [ 0]
[ 0] '02/04/2004' [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] '05/04/2004'
[ 0] [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] '05/04/2004'
[ 0] [ 0] [ 0] '04/04/2004' [ 0]
[ 0] [ 0] [ 0] '04/04/2004' [ 0]
[ 0] [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] [ 0]
'01/04/2004' [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] '03/04/2004' [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] [ 0]
'01/04/2004' [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] '03/04/2004' [ 0] [ 0]
[ 0] [ 0] '03/04/2004' [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] [ 0]
[ 0] [ 0] [ 0] [ 0] '05/04/2004'};
[ii,jj] = find( cellfun('ischar',d));
k = sortrows([ii,jj],1);
out = d(sub2ind(size(d),k(:,1),k(:,2)));

  1 Comments

Hi, I would like to use your solution. Your solutions have been very helpful! This keeps telling me that there is 'Error using cellfun Unknown option.' I'm not sure how to fix that unfortunately...

Sign in to comment.



Translated by