フィルターのクリア

Remove first characters from a string

62 ビュー (過去 30 日間)
Melissa Ette
Melissa Ette 2020 年 10 月 26 日
コメント済み: Stephen23 2020 年 10 月 26 日
I have a celll u(5,1) that looks like :
u{1,1}=''ART1/TEACH''
u{2,1}=''H0ME/SHOW''
I want to remove the first 5 characters from each of these strings so that :
u{1,1}=''TEACH''
u{2,1}=''SHOW''
How can I do it?
  1 件のコメント
Stephen23
Stephen23 2020 年 10 月 26 日
The duplicated single-quotes are not valid MATLAB syntax:
u{1,1} = ''ART1/TEACH''
u{2,1} = ''H0ME/SHOW''
Do you actually have a cell array of character vectors:
u = {'ART1/TEACH';'H0ME/SHOW'}
or a string array?:
u = ["ART1/TEACH";"H0ME/SHOW"];

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

回答 (2 件)

per isakson
per isakson 2020 年 10 月 26 日
編集済み: per isakson 2020 年 10 月 26 日
... or if I understand "I want to remove the first 5 characters from each of these strings" literally
%%
u{1,1} = "ART1/TEACH";
u{2,1} = "H0ME/SHOW";
%%
for jj = 1 : size(u,1)
u{jj,1} = eraseBetween(u{jj,1},1,5);
end
It's recommended to keep strings in string arrays rather than cell arrays.
%%
u(1,1) = "ART1/TEACH";
u(2,1) = "H0ME/SHOW";
%%
u = eraseBetween( u, 1,5 );
inspect
>> u
u =
2×1 string array
"TEACH"
"SHOW"
And a final alternative
%%
u(1,1) = "ART1/TEACH";
u(2,1) = "H0ME/SHOW";
%%
u = extractAfter( u, "/" );

Cris LaPierre
Cris LaPierre 2020 年 10 月 26 日
Perhaps try using cellfun in combination with extract?
u{1,1}="ART1/TEACH";
u{2,1}="H0ME/SHOW"
u = 2x1 cell array
{["ART1/TEACH"]} {["H0ME/SHOW" ]}
f = @(x) extractAfter(x,5);
a=cellfun(f,u,'UniformOutput',false);
a=vertcat(a{:})
a = 2×1 string array
"TEACH" "SHOW"

カテゴリ

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

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by