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

1 回表示 (過去 30 日間)
Ioannis Vourvachakis
Ioannis Vourvachakis 2021 年 11 月 6 日
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
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.
Ioannis Vourvachakis
Ioannis Vourvachakis 2021 年 11 月 6 日
編集済み: Ioannis Vourvachakis 2021 年 11 月 6 日
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.

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2021 年 11 月 6 日
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 件のコメント
Image Analyst
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.
Ioannis Vourvachakis
Ioannis Vourvachakis 2021 年 11 月 6 日
Yes, I accepted it. Thank you again.

サインインしてコメントする。

その他の回答 (1 件)

Paul
Paul 2021 年 11 月 6 日
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'}
  1 件のコメント
Ioannis Vourvachakis
Ioannis Vourvachakis 2021 年 11 月 6 日
Thank you.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by