how to find a common part of the strings?

62 ビュー (過去 30 日間)
abdul kalam
abdul kalam 2016 年 6 月 11 日
編集済み: Sindar 2018 年 7 月 18 日
Hi there!
Could you please help me how to find a common part of the strings?
for example: S1_carbon_avg_air, S1_carbon_err_air, S1_carbon_avg_arg, S1_carbon_err_arg, S1_carbon_avg_nit, S1_carbon_err_nit,
the coomon string is S1_carbon and i want to use this as legend and title of the graph as well.
Thank you in advance
Abdul kalam

採用された回答

Walter Roberson
Walter Roberson 2016 年 6 月 11 日
If you have the strings in a cell array of strings, Scell, then
Schar = char(Scell(:));
all_rows_same = all(diff(Schar == 0, 1),1);
common_cols = find(~all_rows_same, 1, 'first');
if isempty(common_cols)
common_to_use = '?'
else
common_to_use = Scell{1}(1:common_cols);
end
This finds the longest leading substring common to all entries in the cell array and uses that; however if there is no leading substring that is common to all of them then it uses '?' just to have something .
  6 件のコメント
Matthias Brandt
Matthias Brandt 2017 年 6 月 6 日
編集済み: Matthias Brandt 2017 年 6 月 6 日
a combination of both answers above gives an even simpler code: first we create the cell array that the first answer assumes with the help of the second:
s='S1_C2sum_avg_air, S1_C2sum_er_air, S1_C2sum_avg_arg,S1_C2sum_er_arg, S1_C2sum_avg_nit, S1_C2sum_er_nit';
Scell=strsplit(s,', ');
the extraction of the common part can then be written in one (even more intuitive) line:
common_to_use = Scell{1}(all(~diff(char(Scell(:)))))
the string we are after are "the characters that do not differ among all". Be aware, this code can produce false positive characters, if not only the first characters are equal through all the strings (as in the example).
Sindar
Sindar 2018 年 7 月 18 日
編集済み: Sindar 2018 年 7 月 18 日
This finds only the leading match
common_to_use = Scell{1}(1:find(any(diff(char(Scell(:))),1),1,'first')-1)
if isempty(common_to_use)
common_to_use='?'
end

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

その他の回答 (2 件)

dpb
dpb 2016 年 6 月 11 日
The most trivial is that the part thru the second underscore is the same so use that fact...
ix=strfind(str,'_'); % the underscore locations in string 'str'
titleStr=str(1:ix(2)-1)); % the string up to that location
title(titleStr) % use it
  2 件のコメント
abdul kalam
abdul kalam 2016 年 6 月 11 日
編集済み: abdul kalam 2016 年 6 月 11 日
Thanks for your quick reply dpb,
to elaborate,
I have three matrices of 10*17 size. all columns have column headers. first column is same in all (time). Col2 to col9 is intensity and col 10 to col 17 are error bars. I want to plot time vs (coli, coli+8) as error graphs. in a plot i should use 6 columns (col(i), col(i+8)) from 3 matrices) and their corresponding column head strings. I have plotted the graph but stuck at generating legend and a common name (to be used as title).
sometimes the strings could be s1_carbon_avg_air,s1_carbon_err_air,s2_carbon_avg_arg,s2_carbon_err_arg in this the common string is 'carbon'.
Image Analyst
Image Analyst 2016 年 6 月 11 日
Is the "common" part always between underlines? Otherwise you could say that "r" is a common string since the character r is all over the place.

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


Gergö Schmidt
Gergö Schmidt 2017 年 5 月 30 日
If you have a uniform text/delimiter structure you could use the built-in strsplit and ismember functions like this:
Scell = {'S1_carbon_avg_air', 'S1_carbon_err_air', 'S1_carbon_avg_arg', 'S1_carbon_err_arg', 'S1_carbon_avg_nit', 'S1_carbon_err_nit'};
delimiter = '_';
commonparts = strsplit(Scell{1},delimiter);
for iS = 2:numel(Scell)
commonparts(~ismember(commonparts,strsplit(Scell{iS},delimiter))) = [];
end
commonparts = strjoin(commonparts,delimiter)
This will deliver:
commonparts =
S1_carbon
But in case of
Scell = {'S1_carbon_avg_air', 'S1_carbon_err_air'};
it gives
S1_carbon_air
which might be useful for some reason...

カテゴリ

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