【发布时间】: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));
它适用于单个字符串,但不适用于元胞数组。我已经尝试过其他适用于单元格数组的函数,例如strtrim、strtrunc。我想提取最后的数字,但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