Clear Filters
Clear Filters

Replace NaN's in a cell array with blanks (not empty char/string)

1 view (last 30 days)
Hello, I am using R2015. I am reading my data using xlsread. I have a cell array of numbers/strings and NaN's. I need to replace NaN's with blank cells (not empty char/string).
Thanks :)

Accepted Answer

Walter Roberson
Walter Roberson on 29 Oct 2017
mask = cellfun(@(C) isnumeric(C) && isscalar(C) && isnan(C), YourCell);
YourCell(mask) = {[]}; %guessing that "blank cells" means "empty array"
  2 Comments
Walter Roberson
Walter Roberson on 29 Oct 2017
YourCell = cell(10,10);
for K = randperm(100,80); YourCell{K} = randi(5,randi(5),randi(5)); end
for K = randperm(100,20); YourCell{K} = char('A' + randi(10,1,randi(8))); end
for K = randperm(100,10); YourCell{K} = nan; end
CopyOfCell = YourCell;
mask = cellfun(@(C) isnumeric(C) && isscalar(C) && isnan(C), YourCell);
YourCell(mask) = {[]}; %guessing that "blank cells" means "empty array"
Now when I compare CopyOfCell to the modified YourCell I see that NaN has indeed been replaced by 0×0 double. I see no 0x0 char created.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!