sorting columns with empty cells
5 views (last 30 days)
Show older comments
Dear all,'
I have the following column and as you can see the 1rst, 40th, 79th asnd so forth are mising
''
'23-11-2008'
'21-12-2008'
'18-01-2009'
'15-02-2009'
'15-03-2009'
'12-04-2009'
'10-05-2009'
'07-06-2009'
'05-07-2009'
'02-08-2009'
'30-08-2009'
'27-09-2009'
'25-10-2009'
'22-11-2009'
'20-12-2009'
'24-01-2010'
'21-02-2010'
'21-03-2010'
'18-04-2010'
'16-05-2010'
'13-06-2010'
'11-07-2010'
'08-08-2010'
'05-09-2010'
'03-10-2010'
'31-10-2010'
'28-11-2010'
'26-12-2010'
'23-01-2011'
'20-02-2011'
'20-03-2011'
'17-04-2011'
'15-05-2011'
'12-06-2011'
'12-07-2011'
'07-08-2011'
'04-09-2011'
'02-10-2011'
''
'23-11-2008'
'21-12-2008'
'18-01-2009'
'15-02-2009'
'15-03-2009'
'12-04-2009'
'10-05-2009'
'07-06-2009'
'05-07-2009'
'02-08-2009'
'30-08-2009'
'27-09-2009'
'25-10-2009'
'22-11-2009'
'20-12-2009'
'24-01-2010'
'21-02-2010'
'21-03-2010'
'18-04-2010'
'16-05-2010'
'13-06-2010'
'11-07-2010'
'08-08-2010'
'05-09-2010'
'03-10-2010'
'31-10-2010'
'28-11-2010'
'26-12-2010'
'23-01-2011'
'20-02-2011'
'20-03-2011'
'17-04-2011'
'15-05-2011'
'12-06-2011'
'12-07-2011'
'07-08-2011'
'04-09-2011'
'02-10-2011'
''
'23-11-2008'
I want to sort out a bigger matrix say mdata1 using the code
d=datenum(mdata1(:,11),'dd-mm-yyyy');
[l,idx]=sortrows(d);
zoi=mdata1(idx,:);
where mdata1(:,11), is the above column
the problem is that some cells are empty as i said before
and the matlab stucks at command datenu
Is there anything I can to avoid this problem withou creating a fake date for the empty cells?
thanks in advance
0 Comments
Accepted Answer
Andrei Bobrov
on 28 May 2012
d1 = mdata1(:,11);
d1(cellfun('isempty',d1)) = {'00-00-0000'};
[id,id] = sort(datenum(d1,'dd-mm-yyyy'));
out = mdata1(id,11);
More Answers (3)
Walter Roberson
on 27 May 2012
What do you want to have happen for those empty locations?
You can use
is_empty_date = cellfun(@isempty, mdata1(:,11));
and then is_empty_date would be a logical array telling you which were empty or not. You could use that to remove the empties or to move them to the beginning or the end (and you would sortrows() only on the non-empties)
2 Comments
Oleg Komarov
on 27 May 2012
Yes you can, just use the negated is_empty_date to select those which are NOT empty, sort them. Then use is_empty_date to select the empty ones and concatenate with teh sorted at the end or at the beginning.
the cyclist
on 27 May 2012
I encounter this same issue frequently, and don't have a great solution. What I typically do is something like
dateCell = mdata1(:,11);
d = nan(size(dateCell));
nullIndex = strcmp(dateCell,''); % Or whatever character string works.
validIndex = not(nullIndex);
d(validIndex) = datenum(dateCell(validIndex),'mm-dd-yyyy');
Then, you'll have NaNs for the empty dates.
I hope someone has a better solution, because this is ugly! But it works.
Geoff
on 28 May 2012
You don't have to use datenum to sort this data - I find datenum too slow and I generally avoid it. I prefer to represent my textual dates as YYYY-MM-DD because they're ASCII-sortable and readable.
d = char(mdata1(:,11));
[~, idx] = sortrows(d(:, [7:10,3:6,1:2])); % DD-MM-YYYY => YYYY-MM-DD
zoi = mdata1(idx, :);
See Also
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!