Observation: sortrows() blows up when attempting to sort empty cells

This is more of an observation than a question, but it took me two days to figure it out.
I am pre-allocating space for an array, to save time. The array is about 50,000 rows long, and pre-allocating the cells saves a lot of time processing, as opposed to "growing" the array one row at a time. However, if some of the row cells are not populated, the sortrows function will crash when it gets to the rows containing empty cells.
output = cell(5,5);
output =
5x5 cell array
{0x0 double} {0x0 double} {0x0 double} {0x0 double} {0x0 double}
{0x0 double} {0x0 double} {0x0 double} {0x0 double} {0x0 double}
{0x0 double} {0x0 double} {0x0 double} {0x0 double} {0x0 double}
{0x0 double} {0x0 double} {0x0 double} {0x0 double} {0x0 double}
{0x0 double} {0x0 double} {0x0 double} {0x0 double} {0x0 double}
output((1:3),(1:5)) = {'1'}
output =
5x5 cell array
{'1' } {'1' } {'1' } {'1' } {'1' }
{'1' } {'1' } {'1' } {'1' } {'1' }
{'1' } {'1' } {'1' } {'1' } {'1' }
{0x0 double} {0x0 double} {0x0 double} {0x0 double} {0x0 double}
{0x0 double} {0x0 double} {0x0 double} {0x0 double} {0x0 double}
sortrows(output)
Error using sort
Cell elements must be character arrays
I'll admit, a more careful job of coding could prevent this from happening. However, it would be nice if the sort and sortrows functions could just take this in stride, rather than crashing.

5 comentarios

Kurt
Kurt el 8 de Dic. de 2022
Editada: Kurt el 8 de Dic. de 2022
Note: This also happens even if the extra rows are initialized to empty character arrays:
{'1' } {'1' } {'1' } {'1' } {'1' }
{'1' } {'1' } {'1' } {'1' } {'1' }
{'1' } {'1' } {'1' } {'1' } {'1' }
{[] } {[] } {[] } {[] } {[] }
{[] } {[] } {[] } {[] } {[] }
Stephen23
Stephen23 el 8 de Dic. de 2022
Editada: Stephen23 el 8 de Dic. de 2022
"This also happens even if the extra rows are initialized to empty character arrays:"
No, it does not. I have used this exact feature of SORTROWS since atleast R2009b.
What you show are not empty character vectors, but are in fact empty numeric arrays.
"pre-allocating the cells saves a lot of time processing, as opposed to "growing" the array one row at a time."
The you can very easily preallocate with empty character arrays, e.g. using REPMAT or simple assignment:
output = cell(5,5);
output(:) = {''};
... the rest of your code
However, it would be nice if the sort and sortrows functions could just take this in stride, rather than crashing.
First, sort and sortrows do not crash in this circumstance. They throw an error.
Second, what do you propose the functions do to "take this in stride"?
  • Should {'1'} comes before or after {[]} in the sorted output?
  • How about {'1'} versus {1}?
  • {'1'} versus {50}?
  • {'5'} versus {1:10}?
Yes, it does. Just prior to the sort I merged two arrays vertically. Each of the arrays has empty cells at the end, and at the transition where it crashes the data looks exactly like this in the debug editor. The array is defined as '1000x24 cell'.
'22:179:10:45:35.740' '0' '22.759' '53.63' '205.5674' ... (24 columns total)
[] [] [] [] [] ...
Error using matlab.internal.math.cellstrpad
Cell elements must be character arrays
Error in sortrows>sortBackToFrontCell (line 137)
tmp = matlab.internal.math.cellstrpad(A,(I,ack));
Error in sortrows(line 77)
I = SortBackToFront(A, col);
How do I prevent this from happening?
Stephen23
Stephen23 el 8 de Dic. de 2022
Editada: Stephen23 el 8 de Dic. de 2022
"Yes, it does. Just prior to the sort I merged two arrays vertically. Each of the arrays has empty cells at the end, and at the transition where it crashes the data looks exactly like this in the debug editor. The array is defined as '1000x24 cell'."
The empty cells at the end are very clearly numeric, not character.
The error message tells you that the cell content must be character (but are not).

Iniciar sesión para comentar.

 Respuesta aceptada

John D'Errico
John D'Errico el 8 de Dic. de 2022
Editada: John D'Errico el 8 de Dic. de 2022
output = cell(5,5);
output(1:3,1:5) = {'1'};
output(4:5,1:5) = {''}
output = 5×5 cell array
{'1' } {'1' } {'1' } {'1' } {'1' } {'1' } {'1' } {'1' } {'1' } {'1' } {'1' } {'1' } {'1' } {'1' } {'1' } {0×0 char} {0×0 char} {0×0 char} {0×0 char} {0×0 char} {0×0 char} {0×0 char} {0×0 char} {0×0 char} {0×0 char}
sortrows(output)
ans = 5×5 cell array
{0×0 char} {0×0 char} {0×0 char} {0×0 char} {0×0 char} {0×0 char} {0×0 char} {0×0 char} {0×0 char} {0×0 char} {'1' } {'1' } {'1' } {'1' } {'1' } {'1' } {'1' } {'1' } {'1' } {'1' } {'1' } {'1' } {'1' } {'1' } {'1' }
Gosh. I must be using a different version of MATLAB than you. :) It works for me.
Do you see the difference between '' and []?
''
ans = 0×0 empty char array
[]
ans = []
The array you created was partly character, and partly NUMERICAL empty elements. Now go back and read the error message.
Error using sort
Cell elements must be character arrays
That is, even the empty cell elements must be empty characters. If you mix it up, then how should sortrows be able to sort mixed cell arrays?

5 comentarios

I think I see the problem now. I just assumed the empty cell elements were character, which is not the case. So, if I initialize this whole oversized array to {''}, it should work. I think.
Yes. That will fix it. And I admit that it may not have been obvious. empty, is empty. Why should it matter how you write something that describes nothing? :)
Kurt
Kurt el 8 de Dic. de 2022
Editada: Kurt el 8 de Dic. de 2022
(Later): That worked, but now I have a whole bunch of blank lines at the beginning of my sorted output. How do I get rid of them?
I think I found the answer here:
"I just assumed the empty cell elements were character, which is not the case."
And that is exactly what the error message was telling you, for the last two days.
Well, it took me two days just to realize that there were blank rows in this gigantic database. After that, you guys led me to the solution fairly quickly.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Productos

Versión

R2022b

Preguntada:

el 8 de Dic. de 2022

Comentada:

el 8 de Dic. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by