sorting columns with empty cells

3 visualizaciones (últimos 30 días)
antonet
antonet el 27 de Mayo de 2012
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

Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 28 de Mayo de 2012
d1 = mdata1(:,11);
d1(cellfun('isempty',d1)) = {'00-00-0000'};
[id,id] = sort(datenum(d1,'dd-mm-yyyy'));
out = mdata1(id,11);
  2 comentarios
antonet
antonet el 28 de Mayo de 2012
thank you andrei
antonet
antonet el 28 de Mayo de 2012
just a small correction. it should be out = mdata1(id,:);

Iniciar sesión para comentar.

Más respuestas (3)

Walter Roberson
Walter Roberson el 27 de Mayo de 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 comentarios
antonet
antonet el 27 de Mayo de 2012
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
Oleg Komarov
Oleg Komarov el 27 de Mayo de 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.

Iniciar sesión para comentar.


the cyclist
the cyclist el 27 de Mayo de 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.
  2 comentarios
antonet
antonet el 28 de Mayo de 2012
thank you cyclist
antonet
antonet el 28 de Mayo de 2012
your approach is ok as well

Iniciar sesión para comentar.


Geoff
Geoff el 28 de Mayo de 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, :);
  2 comentarios
antonet
antonet el 28 de Mayo de 2012
Thank you Geoff
antonet
antonet el 28 de Mayo de 2012
your approach is ok as well

Iniciar sesión para comentar.

Categorías

Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by