How to replace a character using strrep

5 vues (au cours des 30 derniers jours)
Kanakaiah Jakkula
Kanakaiah Jakkula le 2 Fév 2018
Commenté : Jan le 4 Fév 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'}

Réponse acceptée

Birdman
Birdman le 2 Fév 2018
Modifié(e) : Birdman le 2 Fév 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 commentaires
Kanakaiah Jakkula
Kanakaiah Jakkula le 2 Fév 2018
Modifié(e) : Kanakaiah Jakkula le 2 Fév 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 le 4 Fév 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Acquisition Toolbox Supported Hardware dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by