if a string contains any of the strings from an array

16 ビュー (過去 30 日間)
Vosvi
Vosvi 2018 年 8 月 21 日
回答済み: Kojiro Saito 2018 年 8 月 21 日
I have a str variable below which holds the subject name in the form of "S01_output.xlsx" where '01' part can be any number.
filename='S02_output.xlsx';
substring={'02', '04', '07'};
In an if statement, I need to check if filename contains certain numbers which are found in the array called substring. I tried ismember(filename, substring) but I guess it only works when there is exact match.

採用された回答

Kojiro Saito
Kojiro Saito 2018 年 8 月 21 日
Assuming you're using R2014b, how about the following way with strncmpi ?
filename='S02_output.xlsx';
substring={'02', '04', '07'};
substring2=strcat('S', substring);
tf = strncmpi(filename, substring2, 3)
From R2016b, we can use contains function and it's easier.
filename='S02_output.xlsx';
substring={'02', '04', '07'};
tf = contains(filename, substring)

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2014b

Community Treasure Hunt

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

Start Hunting!

Translated by