Hi. I need to transform these lines:
name_1 = '<Hello World>';
name_2 ='File numb: 5';
into these:
name_1 = 'Hello World';
name_2 ='File numb 5';
In general, I would like to delete, if they are present, within the name all 8 symbols: \ / : * " < > |

 채택된 답변

Matt J
Matt J 2023년 6월 25일

0 개 추천

One way:
name_1 = '<Hello World>';
pat=num2cell('\/:*"<>|');
name_1=erase(name_1,pat)
name_1 = 'Hello World'

추가 답변 (3개)

Matt J
Matt J 2023년 6월 25일
편집: Matt J 2023년 6월 25일

1 개 추천

One way:
name_1 = '<Hello World>';
tf=ismember(name_1, '\/:*"<>|');
name_1(tf)=''
name_1 = 'Hello World'

댓글 수: 1

Alberto Acri
Alberto Acri 2023년 6월 26일
Thank you for your reply. It is as good as the others' answer.

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

Jan
Jan 2023년 6월 25일

1 개 추천

name_1 = '<Hello World>';
name_1 = erase(name_1, ["<", ">", "\", "/", ":", "*", """", "|"])
name_1 = 'Hello World'

댓글 수: 1

Alberto Acri
Alberto Acri 2023년 6월 26일
Thank you for your reply. It is as good as the others' answer.

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

DGM
DGM 2023년 6월 25일
편집: DGM 2023년 6월 25일

0 개 추천

You could also use regexprep(), though perhaps using erase() is more convenient.
% using numbered variables only makes processing more difficult
% instead of embedding implicit indices in the names, just use an array
names = {'<Hello World>';
'File numb: 5';
'1\|2/>3:<4*"5"*6<:7>/8|\9'};
% replace any instance of the listed characters with ''
names = regexprep(names,'[\\/:*"<>|]*','')
names = 3×1 cell array
{'Hello World'} {'File numb 5'} {'123456789' }

댓글 수: 1

Alberto Acri
Alberto Acri 2023년 6월 26일
Thank you for your reply. It is as good as the others' answer.

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

카테고리

도움말 센터 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품

릴리스

R2021b

질문:

2023년 6월 25일

댓글:

2023년 6월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by