Alternative to endsWith function for older Matlab versions

7 ビュー (過去 30 日間)
Fran López
Fran López 2020 年 3 月 5 日
回答済み: Akansha Saxena 2020 年 10 月 21 日
Hello,
I have a cell array with strings in each cell, I would like to obtain the indices of the cells that contain a string ending with a particular word. Before I was using the endsWith function, but I am using an older version of matlab now and I would appreciate any advise.
Example:
A = {'pupil', 'car', 'version', 'onset', 'hello'}
results = endsWith(A,'on')
results is a logical 0 0 1 0 0
Many thanks in advance,
Fran

回答 (1 件)

Akansha Saxena
Akansha Saxena 2020 年 10 月 21 日
pattern ='on';
results = cellfun(@(x) (length(char(x))>length(pattern)) && strcmpi(pattern,x(length(char(x))-(length(pattern)-1):end)),A);

カテゴリ

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