sorting columns with empty cells

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

 采纳的回答

d1 = mdata1(:,11);
d1(cellfun('isempty',d1)) = {'00-00-0000'};
[id,id] = sort(datenum(d1,'dd-mm-yyyy'));
out = mdata1(id,11);

2 个评论

thank you andrei
just a small correction. it should be out = mdata1(id,:);

请先登录,再进行评论。

更多回答(3 个)

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 个评论

tank you walter. So you mean that I can not combine your command with mine to sort out the matrix mdata1 according to 11th column that, in my case should also include the empties.
thanks
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.

请先登录,再进行评论。

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.
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, :);

类别

帮助中心File Exchange 中查找有关 Dates and Time 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by