【问题标题】:Find strings of a cell array in another cell array在另一个元胞数组中查找一个元胞数组的字符串
【发布时间】:2020-08-18 20:42:21
【问题描述】:
var_names = {'X1','X2'};
data_pool = {'AB\CD\X1','AB\CD\X1_A','AB\CD\X1_B','AB\CD\X2','AB\CD\X2_A','AB\CD\X2_B'};

我需要数据的变量的名称是 X1 和 X2。数据池中有名称相似的变量; '_A' 和 '_B' 以及字符串都有路径名,因此 data_pool 中的字符串总是比 var_names 中的长。

我需要将 data_pool 缩减为以下内容:

var_names_new = {'AB\CD\X1','AB\CD\X2'};

只有 X1 和 X2 的完整路径,而不是其他后缀路径。

【问题讨论】:

    标签: string matlab cell-array


    【解决方案1】:
    result = data_pool(endsWith(data_pool, strcat('\', var_names)));
    

    它的工作原理如下:

    1. strcat'\' 附加到var_names 中的每个字符串;
    2. endsWithdata_pool 中生成一个以上一步中的任何字符串结尾的字符串的逻辑索引;
    3. 索引到 data_pool 会产生所需的结果。

    【讨论】:

      【解决方案2】:

      您需要检查当前的data_pool 变量是否包含X1X2 值。

      result = {};
      
      count = 0;
      for i=1:length(data_pool)
          for j=1:length(var_names)
              new_pool = strrep(string(data_pool(i)), 'ABCD\', '');
              if contains(string(var_names(j)), new_pool)
                  count = count+1;
                  val = strcat('ABCD\',new_pool);
                  result{1, count} = val;
              end
          end
      end
      

      结果:

      ["ABCD\X1"]    ["ABCD\X2"]
      

      【讨论】:

        猜你喜欢
        • 2015-04-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-27
        • 2011-07-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多