【问题标题】:MATLAB’s extractBetween alternative for Octave in cell arrayMATLAB extractBetween 替代元胞数组中的 Octave
【发布时间】:2019-10-12 12:11:05
【问题描述】:

我正在尝试在 Octave 中运行 MATLAB 代码。有一个 MATLAB 函数 extractBetween 在 octave 中尚不可用。原代码是

numbers = str2double(extractBetween(dirAndFileNames(:,2), 4, 5));

我已尝试用此代码替换它。

numbers = str2double(substr(dirAndFileNames(:,2), 4, 2));

它适用于单个字符串,但不适用于元胞数组。我已经尝试过其他适用于单元格数组的函数,例如strtrimstrtrunc。我想提取最后的数字,但strtrunc 给出的结果与我想要的相反。它给出了第一个字母。

dirAndFilenames(:,2) 看起来像这样:

debug> dirAndFileNames(:,2)
ans =
{
  [1,1] = desktop.ini
  [2,1] = trn01
  [3,1] = trn02
  [4,1] = trn03
  [5,1] = trn04
  [6,1] = trn05
  [7,1] = trn06
  [8,1] = trn07
  [9,1] = trn08
  [10,1] = trn09
  [11,1] = trn10
}

【问题讨论】:

  • 您是否尝试在元胞数组中的字符串上编写循环?如果要隐藏显式循环,可以使用函数cellfun
  • 我试过了,但我是 matlab 代码的新手。
  • for i = 1:length(dirAndFileNames) numbers{i} = str2double(substr(dirAndFileNames(i,2){1}, 4, 2)) end

标签: matlab octave compatibility substr cell-array


【解决方案1】:

正如@cris 建议的那样,我不得不循环。

for i = 1:length(dirAndFileNames) 
    numbers{i} = str2double(substr(dirAndFileNames(i,2){1}, 4, 2)) 
end

【讨论】:

    【解决方案2】:

    您可以使用cellindexmat

    a = dirAndFileNames(:, 2);
    result = cellindexmat(a, 4:5);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-08
      相关资源
      最近更新 更多