How to remove rows of empty strings?

조회 수: 49 (최근 30일)
Calvin Ewanchuk
Calvin Ewanchuk 2019년 10월 30일
편집: the cyclist 2019년 10월 30일
I have a string array which I have preallocated as follows:
BusName = strings(MaxB,NumBVF,1);
After reading in the data, my string array has many empty rows at the end.
So my question is: How do I remove all rows that consist entirely of blank strings ("")?
Here is what I've come up with so far:
BusName(strcmp(BusName(:,2),""),:) = [];
Unfortunately, it is removing rows that it shouldn't be (rows that have "" but also data).
Any suggestions (and explanation) will be greatly appreciated!
  댓글 수: 3
Calvin Ewanchuk
Calvin Ewanchuk 2019년 10월 30일
Never uploaded a file here before, but here goes
the cyclist
the cyclist 2019년 10월 30일
편집: the cyclist 2019년 10월 30일
Your upload worked! (And my solution works on your input.)

댓글을 달려면 로그인하십시오.

답변 (2개)

the cyclist
the cyclist 2019년 10월 30일
편집: the cyclist 2019년 10월 30일
I'm having trouble following the logic in the command you posted, because you seem to be checking only the second column of Busname.
But I'm pretty sure you'll want to use the all command to get the rows where all the elements are blank.
I think it would be something like
BusName(all(strcmp(BusName,""),2),:) = [];
  댓글 수: 1
Calvin Ewanchuk
Calvin Ewanchuk 2019년 10월 30일
편집: Calvin Ewanchuk 2019년 10월 30일
Sorry, I'm kinda new to MATLAB. I'll try your code.
Yes, I want to delete the rows where all the strings are blank (""). I just can't figure out the syntax.

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2019년 10월 30일
편집: the cyclist 2019년 10월 30일
BusName = rmmissing(BusName, 'MinNumMissing', size(BusName,2));
  댓글 수: 2
Calvin Ewanchuk
Calvin Ewanchuk 2019년 10월 30일
I don't think this will work since the data is blank ("') but not <missing>.
the cyclist
the cyclist 2019년 10월 30일
Yeah, does not work on your string array.
(Walter, my edit of your answer was just to fix up 'MinMissing' to 'MinNumMissing').

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by