strtrim not working for cell array
显示 更早的评论
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 个评论
Image Analyst
2016-2-15
Geoff Hayes
2016-2-15
Praveen - please describe the contents for this cell array. Are all elements strings or do some include numeric elements? Based on the error message, I am assuming that the latter is true. If that is the case, you will have to apply strtrim to just those elements of the cell array that are strings.
Praveen Choudhury
2016-2-15
Geoff Hayes
2016-2-15
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).
采纳的回答
更多回答(1 个)
Sean de Wolski
2016-2-15
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);
类别
在 帮助中心 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!