define a pattern to use with dir and get the file names containing numbers

35 次查看(过去 30 天)
Hi
I need to 'dir' a folder containing files that has only numbers in their file name:
165454.txt
216541ffgb.txt
In this case, I want to see only the first file in my 'dir' result.
One way is to dir and then filter them using pattern but I do not want to do it.

采纳的回答

Jan
Jan 2022-2-24
编辑:Jan 2022-2-24
This does not work with the patterns of the operating system.
Use a regexp call to filter the names instead:
List = dir('*.txt'):
Name = {List.Name};
% Name = {'1234.txt', '1234s.txt', '1265', 'a1234.txt'};
matchC = regexp(Name, '^\d+\.txt', 'start') % Or: '^[0-9]+\.txt'
match = ~cellfun('isempty', matchC)
List = List(match);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

产品


版本

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by