Filter löschen
Filter löschen

How to replace a character using strrep

1 Ansicht (letzte 30 Tage)
Kanakaiah Jakkula
Kanakaiah Jakkula am 2 Feb. 2018
Kommentiert: Jan am 4 Feb. 2018
Hi,
I have below cell array(contain string of characters and double), I want to replace "/" with "*" if exsit "/" in the name. My matlab version is R2012b. I give me error when the name is numeric: Error is: Cell elements must be char.
Input: {'PV05K/THF05/34' '40';'5' 'PV05K/THH12';'TjK05K' 'ZV45E/FGF47';'2' 'ZV45E/FGF47';'ZV95G/FGGF' 'ZA77G/KKGGF'}

Akzeptierte Antwort

Birdman
Birdman am 2 Feb. 2018
Bearbeitet: Birdman am 2 Feb. 2018
regexprep or strrep will do it with only one line of code:
str={'PV05K/THF05/34' '40';'5' 'PV05K/THH12';'TjK05K' 'ZV45E/FGF47';'2' 'ZV45E/FGF47';'ZV95G/FGGF' 'ZA77G/KKGGF'}
strNew=regexprep(str,'/','*')
or
strNew=strrep(str,'/','*')
  3 Kommentare
Kanakaiah Jakkula
Kanakaiah Jakkula am 2 Feb. 2018
Bearbeitet: Kanakaiah Jakkula am 2 Feb. 2018
Sir,
Initially, I used as below (in Matlab2012b): Input(:,1:2)=strrep(Input(:,1:2),'/','*'), But It give me error: "Cell elements must be char" I traced that the error occured where the cell elements are numeric.
Jan
Jan am 4 Feb. 2018
@Kanakaiah Jakkula: Exactly. strrep works on cell strings only. Your example data contains strings only. If this is not the case in the real data, please post a meaningful example.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Data Acquisition Toolbox Supported Hardware 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