提取文件名和分机。来自字符串元胞数组
Extract filenames and ext. from cell array of strings
我想从包含文件名的字符串元胞数组中删除目录部分。当然,一种方法是遍历单元格数组并使用 fileparts
但我有超过 1e5 个文件,速度真的很重要。
我目前的做法是:
fns = {"/usr/local/foo.lib", "~/baz.m", "home/rms/eula.txt", "bar.m"}
filenames = cellfun (@(fn, s) fn(s+1:end), fns,
num2cell (rindex (fns, filesep())),
"UniformOutput", false)
给出了所需的输出:
fns =
{
[1,1] = /usr/local/foo.lib
[1,2] = ~/baz.m
[1,3] = home/rms/eula.txt
[1,4] = bar.m
}
filenames =
{
[1,1] = foo.lib
[1,2] = baz.m
[1,3] = eula.txt
[1,4] = bar.m
}
每个文件大约需要 2e-5 秒。有没有更好(更快、更易读)的方法来做到这一点?
编辑 我已经添加了 Sardars 解决方案和我之前使用正则表达式的尝试以及一些基准测试结果:
fns = {"/usr/local/foo.lib", "~/baz.m", "home/rms/eula.txt", "bar.m"};
fns = repmat (fns, 1, 1e4);
tic
f1 = cellfun (@(fn, s) fn(s+1:end), fns,
num2cell (rindex (fns, "/")),
"UniformOutput", false);
toc
tic
[~, ~, ~, M] = regexp (fns, "[^\/]+$", "lineanchors");
f2 = cell2mat (M);
toc
tic
## Asnwer from Sardar Usama
f3 = regexprep(fns, '.*/', '');
toc
assert (f1, f2)
assert (f1, f3)
给出
Elapsed time is 0.729995 seconds. (Original code with cellfun)
Elapsed time is 0.67545 seconds. (using regexp)
Elapsed time is 0.230487 seconds. (using regexprep)
使用 regexprep
搜索字符串直到最后一个 /
并用空字符串替换出现的字符串。
filenames = regexprep(fns, '.*/', '');
我想从包含文件名的字符串元胞数组中删除目录部分。当然,一种方法是遍历单元格数组并使用 fileparts
但我有超过 1e5 个文件,速度真的很重要。
我目前的做法是:
fns = {"/usr/local/foo.lib", "~/baz.m", "home/rms/eula.txt", "bar.m"}
filenames = cellfun (@(fn, s) fn(s+1:end), fns,
num2cell (rindex (fns, filesep())),
"UniformOutput", false)
给出了所需的输出:
fns =
{
[1,1] = /usr/local/foo.lib
[1,2] = ~/baz.m
[1,3] = home/rms/eula.txt
[1,4] = bar.m
}
filenames =
{
[1,1] = foo.lib
[1,2] = baz.m
[1,3] = eula.txt
[1,4] = bar.m
}
每个文件大约需要 2e-5 秒。有没有更好(更快、更易读)的方法来做到这一点?
编辑 我已经添加了 Sardars 解决方案和我之前使用正则表达式的尝试以及一些基准测试结果:
fns = {"/usr/local/foo.lib", "~/baz.m", "home/rms/eula.txt", "bar.m"};
fns = repmat (fns, 1, 1e4);
tic
f1 = cellfun (@(fn, s) fn(s+1:end), fns,
num2cell (rindex (fns, "/")),
"UniformOutput", false);
toc
tic
[~, ~, ~, M] = regexp (fns, "[^\/]+$", "lineanchors");
f2 = cell2mat (M);
toc
tic
## Asnwer from Sardar Usama
f3 = regexprep(fns, '.*/', '');
toc
assert (f1, f2)
assert (f1, f3)
给出
Elapsed time is 0.729995 seconds. (Original code with cellfun)
Elapsed time is 0.67545 seconds. (using regexp)
Elapsed time is 0.230487 seconds. (using regexprep)
使用 regexprep
搜索字符串直到最后一个 /
并用空字符串替换出现的字符串。
filenames = regexprep(fns, '.*/', '');