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

4 ビュー (過去 30 日間)
Kurt
Kurt 2022 年 12 月 8 日
コメント済み: Kurt 2022 年 12 月 8 日
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 件のコメント
Kurt
Kurt 2022 年 12 月 8 日
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 2022 年 12 月 8 日
編集済み: Stephen23 2022 年 12 月 8 日
"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).

サインインしてコメントする。

採用された回答

John D'Errico
John D'Errico 2022 年 12 月 8 日
編集済み: John D'Errico 2022 年 12 月 8 日
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 件のコメント
Stephen23
Stephen23 2022 年 12 月 8 日
"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.
Kurt
Kurt 2022 年 12 月 8 日
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by