Finding cells with specific string in cell array and substituting them
21 次查看(过去 30 天)
显示 更早的评论
In a cell array named CC I search for a specific string, ' -' and I want to substitute the contents of all those cells with something else, e.g. '0'. When I perform the search using
[ii jj]=find(strncmpi(CC,' -',2))
I get the resulots in the form:
ii= 1
2
3
4
8
and
jj= 12
12
12
15
15
Now: how can I change the elements containg that string and having indexes ii & jj with '0'? I tried different form but cannot find a way to refer to elements of CC having ii & jj as row and column number.
0 个评论
采纳的回答
Jos (10584)
2017-12-19
You can simply use strrep to replace strings in a cell array:
CC = {'A',' -','B',' -','C'}
CCout = strrep(CC,' -', '0')
2 个评论
Jos (10584)
2017-12-19
Ah, your cell is a mixture of strings and numbers ...
CC(strcmpi(CC,' -')) = {0}
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Cell Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!