Finding cells with specific string in cell array and substituting them
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Saeid
am 19 Dez. 2017
Kommentiert: Saeid
am 20 Dez. 2017
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 Kommentare
Akzeptierte Antwort
Jos (10584)
am 19 Dez. 2017
You can simply use strrep to replace strings in a cell array:
CC = {'A',' -','B',' -','C'}
CCout = strrep(CC,' -', '0')
2 Kommentare
Jos (10584)
am 19 Dez. 2017
Ah, your cell is a mixture of strings and numbers ...
CC(strcmpi(CC,' -')) = {0}
Weitere Antworten (1)
Stephen23
am 19 Dez. 2017
Using find is not required, and just makes it much more complex. All you need is to use logical indexing:
idx = strncmpi(CC,' -',2);
CC(idx) = {'0'};
4 Kommentare
Stephen23
am 19 Dez. 2017
Putting a scalar numeric into square brackets is pointless. All you need is simply {0}.
Siehe auch
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!
