How to replace a character using strrep

조회 수: 3 (최근 30일)
Kanakaiah Jakkula
Kanakaiah Jakkula 2018년 2월 2일
댓글: Jan 2018년 2월 4일
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'}

채택된 답변

Birdman
Birdman 2018년 2월 2일
편집: Birdman 2018년 2월 2일
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
Kanakaiah Jakkula
Kanakaiah Jakkula 2018년 2월 2일
편집: Kanakaiah Jakkula 2018년 2월 2일
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 2018년 2월 4일
@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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by