필터 지우기
필터 지우기

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

조회 수: 1 (최근 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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by