strtrim not working for cell array

5 Ansichten (letzte 30 Tage)
Praveen Choudhury
Praveen Choudhury am 15 Feb. 2016
Beantwortet: Walter Roberson am 15 Feb. 2016
I have a cell of 472*12 size. I am trying to use strtrim on this cell but I am getting an error which says "Input should be a string or a cell array of strings." Can anyone help me with this?
  4 Kommentare
Praveen Choudhury
Praveen Choudhury am 15 Feb. 2016
All my elements are of type char. Please see the attached.
Geoff Hayes
Geoff Hayes am 15 Feb. 2016
They appear to be either character strings or are empty [] elements. See Sean's answer below for how to deal with this situation (his code replaces the empty elements with '' strings).

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 15 Feb. 2016
[] is not of type char so you are current attempting to use strtrim on non-strings.
Here is a workaround:
new_tableData = cellfun(@(S) strtrim([S '']), tableData, 'Uniform', 0);
The [] of '' has no effect on strings, but has the subtle side effect of converting [] into '' which can be trimmed.

Weitere Antworten (1)

Sean de Wolski
Sean de Wolski am 15 Feb. 2016
iscellstr(your_cell)
will definitively tell you if your cell contains only strings. It's also possible that it contains numeric empties [] but otherwise strings. If this is the case the following will fix it.
empties = cellfun(@isempty,your_cell)
your_cell(empties) = {''};
Note you could also create the cell this way to begin with rather than using cell()
your_cell = repmat({''},rows,cols);

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by