'ab[c]' to this 'ab_c' ?
조회 수: 1 (최근 30일)
이전 댓글 표시
How can I convert this 'ab[c]' to this 'ab_c' ?
The letters a,b,c are for the specific example.
In the position of c could be any letter.
댓글 수: 2
Steven Lord
2021년 11월 6일
편집: Steven Lord
2021년 11월 6일
Are you trying to change a char vector containing this literal text, or are you trying to change indexing into a variable to instead have numbered names for variables?
If the latter, can you define variables with numbered names like ab_1, ab_2, ab_3, ... ? Yes. Should you do this? Generally we recommend against it. See that page for alternatives you should use instead.
채택된 답변
Image Analyst
2021년 11월 6일
Try this:
s = 'ab[c]'
s = strrep(s, '[', '_') % Replace [ with underline.
s = strrep(s, ']', '') % Replace ] with null.
댓글 수: 3
Image Analyst
2021년 11월 6일
For a cell array, it's the same code:
s = {'ab[c]'; 'xy[z]'}
s = strrep(s, '[', '_') % Replace [ with underline.
s = strrep(s, ']', '') % Replace ] with null.
If this answers your question, maybe you can click the "Accept this answer" link, unless you want to wait for a better one.
추가 답변 (1개)
Paul
2021년 11월 6일
C = { '10fthf[c]'
'10fthf[h]'
'10fthf[m]'
'10fthf[x]'}
cellstr(extractBefore(string(C),"[") + "_" + extractBefore(extractAfter(string(C),"["),2))
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!