Clear Filters
Clear Filters

How to replace a character using strrep

1 view (last 30 days)
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'}

Accepted Answer

Birdman on 2 Feb 2018
Edited: Birdman on 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'}
Kanakaiah Jakkula
Kanakaiah Jakkula on 2 Feb 2018
Edited: Kanakaiah Jakkula on 2 Feb 2018
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 on 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.

Sign in to comment.

More Answers (0)


Find more on Data Acquisition Toolbox Supported Hardware in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!