help with "dir" command
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Image Analyst
2015-2-25
See code samples in the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F
0 个评论
更多回答(1 个)
Stephen23
2015-2-25
编辑:Stephen23
2015-2-25
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 个评论
Image Analyst
2015-2-25
The sorting takes place inside the loop? What sort of sorting are you doing (no pun intended)?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!