How to get common sub string in cell array string?

21 ビュー (過去 30 日間)
galaxy
galaxy 2020 年 3 月 17 日
コメント済み: galaxy 2020 年 3 月 17 日
Example:
cell_arr =
3×1 cell array
{'A.A1.A11.A111' }
{'A.A1.A12.A121' }
{'Event_State.Chart.A.A1.A12'}
How to get output = A.A1 ?

採用された回答

Stephen23
Stephen23 2020 年 3 月 17 日
編集済み: Stephen23 2020 年 3 月 17 日
1- Download and unzip John D'Errico's FEX submission
2- Modify commonsubstring by replacing every
regexp(..., cellsubstr)
with
regexp(..., regexptranslate('escape',cellsubstr))
3- Call commonsubstrings in a loop, e.g.:
C = {'A.A1.A11.A111','A.A1.A12.A121','Event_State.Chart.A.A1.A12'};
S = C{1};
for k = 2:numel(C)
S = commonsubstring(S,C{k});
end
This returns the longest common substring (your output example is not the longest common substring):
>> S
S = A.A1.A1
  1 件のコメント
galaxy
galaxy 2020 年 3 月 17 日
Thank you for your anwser.
Your anser is same exactly my code. :))

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

その他の回答 (1 件)

Bhaskar R
Bhaskar R 2020 年 3 月 17 日
編集済み: Bhaskar R 2020 年 3 月 17 日
fund_cell = regexp(cell_arr , 'A.A1', 'match');
result = [fund_cell{:}]
  1 件のコメント
galaxy
galaxy 2020 年 3 月 17 日
I see, but that is example only.
We don't know what contains in cell_arr.
> str_common = getStrCommon(cell_arr)
>
> ans = A.A1
Already many function for get common string in 2 strings as following:
But I want it more generality.

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

カテゴリ

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