delete empty cells of data array

조회 수: 5 (최근 30일)
JB
JB 2017년 8월 28일
답변: Guillaume 2017년 8월 31일
This might be vary basic but I still need a bit of help to figure this out. I have a relatively large cell array
data={411x7cell 411x7cell 411x7cell 411x7cell 411x7cell ' '}
with data{1,6} being empty. How do I remove/delete this cell from the array. I tried
data(strcmp(' ',data)) = [];
But without luck. Please help... THANKS
  댓글 수: 5
Stephen23
Stephen23 2017년 8월 28일
편집: Stephen23 2017년 8월 28일
The question originally showed a 1x1 character, now you have a 1x1 cell array containing an empty string. Please actually be precise and define the exact specification of what you want. Changing the specifications does not make it easy to help you.
JB
JB 2017년 8월 28일
I want to delete the 1x1 cell array containing an empty string. Alternatively I can replace the '' with whatever I want like NaN, but the overall aim is to delete the empty string (or NaN string). I hope this help as I am very new to matlab coding.

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

답변 (2개)

Guillaume
Guillaume 2017년 8월 31일
@JB, Stephen's comment is spot on. It may not seem like a big deal to you but your language and your syntax is very imprecise and makes it difficult to answer the question approprietely. There is a big difference between:
data={411x7cell 411x7cell 411x7cell 411x7cell 411x7cell ' '}
and
test = { {'a', 'b', 'c'}, {'d', 'e', 'f'},{''}}
The former is a cell array where some cells may be empty (the others containing cell arrays). Removing those empty cells is trivial:
data(cellfun('isempty', data)) = [];
The latter is a cell array where no cell is ever empty, all cells being themselves cell arrays. Some of these subcell arrays may be empty. Removing these empty cell arrays within cell array is a bit more complicated, since you now need to dig through two cell arrays to test for emptyness:
test(cellfun(@(c) all(cellfun('isempty', c)), test) = [];

Stalin Samuel
Stalin Samuel 2017년 8월 28일
Similar discussions
  댓글 수: 2
JB
JB 2017년 8월 28일
I cant get i to work. My problem is more how to delete substring within string (I think).
Stalin Samuel
Stalin Samuel 2017년 8월 31일
편집: Stalin Samuel 2017년 8월 31일
You may get some idea from below code
s={'dsfd ' ,'sdfsdf sdfsd sdfsd',' sdfsdfs'}
newStr = erase(s," ")%removes empty spaces

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

카테고리

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

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by