Hello all,
I have a folder containing 9 .htk files. I need to use "dir", and then "readhtk" in a loop to import them to MATLAB, but DIR appears to give 10 files instead of 9! here is my code:
htkfiles = dir('/Users/Desktop/Acsegment/mfcdir/*.htk');
nhtkfiles = length(htkfiles); % 10!!! It should be 9 tough!
data = cell(nhtkfiles,2);
for k = 1:nhtkfiles
b(k,1) = strcat({'/Users/Desktop/Acsegment/mfcdir/'},{htkfiles(k,1).name});
eval(['data{k,1} = readhtk(b{k,1});']);
end
When looking at the filenames in htkfiles, I have them like this:
htkfiles(1,1).name = '.htk'
htkfiles(2,1).name = 'fadg0_si1279.htk'
htkfiles(3,1).name = 'fadg0_si1909.htk'
htkfiles(4,1).name = 'fadg0_si649.htk'
htkfiles(5,1).name = 'fadg0_sx109.htk'
htkfiles(6,1).name = 'fadg0_sx19.htk'
htkfiles(7,1).name = 'fadg0_sx199.htk'
htkfiles(8,1).name = 'fadg0_sx289.htk'
htkfiles(9,1).name = 'fadg0_sx379.htk'
htkfiles(10,1).name = 'faks0_si943.htk'
Comparing to what I see in that folder, the first file is not supposed to be there! Anyone got any ideas why Im getting one extra file?

댓글 수: 2

per isakson
per isakson 2014년 12월 18일
편집: per isakson 2014년 12월 18일
I cannot reproduce the issue on R2013a, Win7, 64bit. However, .htk seems to be a legal file name. Are you sure there isn't a file with that name on the disk.
As others have said, if dir returns that file that's most likely because the file is there.
However, I wamted to comment on your example. You don't need a loop to generate b:
b = strcat(''/Users/Desktop/Acsegment/mfcdir/', {htkfiles.name});
And more importantly, don't use eval to just call a function. You can also avoid the loop using cellfun, in which case you don't even need to predeclare data:
data = cellfun(@readhtk, b, 'UniformOutput', false);

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

 채택된 답변

Guillaume
Guillaume 2014년 12월 18일
편집: Guillaume 2014년 12월 18일

0 개 추천

If all you want to do is remove files starting with '.', it's simply
htkfiles = dir('/Users/Desktop/Acsegment/mfcdir/*.htk');
htkfiles(strncmp({htkfiles.name}, '.', 1)) = []; %remove files and dir starting with '.'
Or, to remove directory as well (a good idea):
htkfiles(strncmp({htkfiles.name}, '.', 1) | [htkfiles.isdir]) = []; %remove directory from list as well
As per my comment, you can then continue with:
htkfullpath = strcat('/Users/Desktop/Acsegment/mfcdir/', {htkfiles.name});
data = cellfun(@readghtk, htkfullpath, 'UniformOutput', false);

추가 답변 (1개)

Orion
Orion 2014년 12월 18일

0 개 추천

Hello,
isn't there a hidden file in your folder ? ( '.htk' )
it's still weird because a file without a name and just an extension shouldn't be accepted by windows.
Otherwise, you can identify your files ?
htkfiles = dir('/Users/Desktop/Acsegment/mfcdir/fa*.htk'); % add _fa_ to find all .htk files beginning with it

댓글 수: 5

Negar
Negar 2014년 12월 18일
Hi, Im not sure about hidden files, Im not working on windows, Im on Mac OS (UNIX), could it be therefor? regarding your last suggestion, I actually have many many files (1344), with various names, not all starting with 'fa'. its just an excerpt I have included here. But how could I see if there is some hidden files in that folder? and how to exclude them? Thank you!
Negar
Negar 2014년 12월 18일
But how can I filter out that filename which starts with '.'?
Titus Edelhofer
Titus Edelhofer 2014년 12월 18일
Hi,
files starting with . are filtered out if you do a "ls" in a shell. Try "ls -a" and you will see the file (or folder) .hks. If it's a folder, take a look inside, if it contains some configurations.
I suspect it's a file that was somehow accidentially created (probably empty). In this case delete it ;-).
Titus
per isakson
per isakson 2014년 12월 18일
편집: per isakson 2014년 12월 18일
  • On Windows the version control program, Mercurial, uses folders named .hg
  • IIRC FEX-submissions from the mac-world includes folders, the names of which have a period in the first position. Possibly those folders are hidden on the mac. Yes, I'm guessing. Check whether htkfiles.isdir is true
"ls -a" gives me 9 files, the .htk file is not included.

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

카테고리

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

태그

질문:

2014년 12월 18일

댓글:

2014년 12월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by