Thank you for your time,this is a question from a Matlab beginer!
I have a folder(name Site) with two hundereds seventy one '.dat' files in it. They named as site●●▲▲.dat ●● starts from 01 up to 09 and ▲▲is from 01 upto 16. Now I need to select just sites with number 0901 to 0916.(i.e. site0901.dat site0902.dat ... site0916.dat) If i write
idir = './Site/';
Fault = [];
files = dir([idir,'*09*.dat']);
I will get site0901.dat as well as site0109.dat
files = dir([idir,'09*.dat']);
wont extract any files.and
files = dir([idir,'*.dat']);
give me all the 271 files.
would you please teach me how to do this.

댓글 수: 2

dir(fullfile(idir, 'site09*.dat'))
Samaneh Arzpeima
Samaneh Arzpeima 2018년 11월 11일
편집: Samaneh Arzpeima 2018년 11월 12일
Thank you So Much.
got exactly what I needed.

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

답변 (1개)

KSSV
KSSV 2018년 11월 9일

0 개 추천

YOu can read all the files using:
allfiles = dir('*.dat') ; % all dat files info
files = [allfiles.name] ; % this gives only file names
From files, you can get the index of your required files like below:
files = {'site01.dat','site02.dat','site03.dat','site04.dat','site05.dat','site06.dat','site07.dat','site08.dat'.....
'site09.dat','site10.dat'} ;
num = cellfun(@(x) x(5:end-4),files,'UniformOutput',false) ;
num = cellfun(@str2num,num) ;
idx = num >5 & num < 9 ;
files(idx)

댓글 수: 5

Samaneh Arzpeima
Samaneh Arzpeima 2018년 11월 11일
Thank you KSSV for your response. I don't know what exactly the problem is ,but this code didn't give me the right answer,the one that I supposed to get.
files = {allfiles.name};
Samaneh Arzpeima
Samaneh Arzpeima 2018년 11월 12일
thank you Walter Roberson My file names are lik site0101.dat site0201.dat site 0301.dat , so with KSSV'solution, those will change to site101.dat site201.dat site301.dat. it wont work for the name that starts with Zero
Walter Roberson
Walter Roberson 2018년 11월 12일
idx = nun>=900&num<=916
Samaneh Arzpeima
Samaneh Arzpeima 2018년 11월 12일
I learned a ton today! Thank’s a lot!

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

카테고리

도움말 센터File Exchange에서 Mathematics에 대해 자세히 알아보기

제품

질문:

2018년 11월 9일

댓글:

2018년 11월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by