Finding cells with specific string in cell array and substituting them
조회 수: 24 (최근 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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/167864/image.png)
댓글 수: 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}
추가 답변 (1개)
참고 항목
카테고리
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!