'ab[c]' to this 'ab_c' ?

2 views (last 30 days)
Ioannis Vourvachakis
Ioannis Vourvachakis on 6 Nov 2021
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 Comments
Ioannis Vourvachakis
Ioannis Vourvachakis on 6 Nov 2021
Edited: Ioannis Vourvachakis on 6 Nov 2021
I want to convert this cell array
'10fthf[c]'
'10fthf[h]'
'10fthf[m]'
'10fthf[x]' .
to this cell array
'10fthf_c'
'10fthf_h'
'10fthf_m'
'10fthf_x'
Ιn other words, I want to create a for loop, where for every element of the first cell array, it will remove the brackets ([]}and put a underscore (_) before the letter that it was previously contained in the brackets.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 6 Nov 2021
Try this:
s = 'ab[c]'
s = 'ab[c]'
s = strrep(s, '[', '_') % Replace [ with underline.
s = 'ab_c]'
s = strrep(s, ']', '') % Replace ] with null.
s = 'ab_c'
  3 Comments
Ioannis Vourvachakis
Ioannis Vourvachakis on 6 Nov 2021
Yes, I accepted it. Thank you again.

Sign in to comment.

More Answers (1)

Paul
Paul on 6 Nov 2021
C = { '10fthf[c]'
'10fthf[h]'
'10fthf[m]'
'10fthf[x]'}
C = 4×1 cell array
{'10fthf[c]'} {'10fthf[h]'} {'10fthf[m]'} {'10fthf[x]'}
cellstr(extractBefore(string(C),"[") + "_" + extractBefore(extractAfter(string(C),"["),2))
ans = 4×1 cell array
{'10fthf_c'} {'10fthf_h'} {'10fthf_m'} {'10fthf_x'}

Community Treasure Hunt

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

Start Hunting!