help with "dir" command

조회 수: 3 (최근 30일)
ludvikjahn
ludvikjahn 2015년 2월 25일
편집: Stephen23 2015년 2월 25일
good morning I have a problem with the command "dir":
I want to find some files which names are f.e. : 10.txt,11.txt,12.txt, etc. and list them within a cell or an array
I tried with this:
for i=10:20
file = dir(fullfile(open_folder, ['*' i '*' '.txt']))
end
neither
file(i) = dir(fullfile(open_folder, ['*' i '*' '.txt']))
works (mismatched dimension or something similar).
the resulting structure is just the file with the last numerical value (i.e. '20.txt' in this case)
is there a possibility to have a cell, or a matrix with all the files found?
Thanks

채택된 답변

Image Analyst
Image Analyst 2015년 2월 25일

추가 답변 (1개)

Stephen23
Stephen23 2015년 2월 25일
편집: Stephen23 2015년 2월 25일
Rather than doing this in a loop, why not use dir 's ability to return multiple matches:
out = dir(fullfile(open_folder,'*.txt'))
This is much neater, faster, and less likely to have bugs than trying to do this in a loop. The usage of fullfile is good code though, so we will keep that!
If required you can sort/filter the strings in out as well, to remove other files that you do not want. You could use regexp for this.
  댓글 수: 2
ludvikjahn
ludvikjahn 2015년 2월 25일
편집: ludvikjahn 2015년 2월 25일
The problem is that I have a database of many Gigabytes of files '.txt' where I have to sort them and I wanted to extract only those referred to a short period of time.
Image Analyst
Image Analyst 2015년 2월 25일
The sorting takes place inside the loop? What sort of sorting are you doing (no pun intended)?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by