function call

To tell my function to perform it's operations on files in a directory Function('directory') is the command for that. how do I tell this function to perform it's operations on only the first n files in that directory and not all of the files.

답변 (2개)

Grzegorz Knor
Grzegorz Knor 2011년 11월 14일

0 개 추천

Something like that:
function my_fun(directory,n)
a = dir(directory);
% a(1) is folder '.'
% a(2) is folder '..'
% so you have to start from a(3)
for k=3:n+2
disp(a(k).name)
% do more operation on folder/file
end

댓글 수: 5

Titus Edelhofer
Titus Edelhofer 2011년 11월 14일
Hi Grzegorz,
note though, that '.' and '..' need not always be the first two entries, I don't know how I managed but I had once stumbled across a folder where the first entry was some weirdly named folder (and therefore . and .. came second and thirs).
Titus
Grzegorz Knor
Grzegorz Knor 2011년 11월 14일
Thank you for the advice! I thought that "." and ".." are always first.
Daniel Shub
Daniel Shub 2011년 11월 14일
I don't know the specifics of dir/ls sorting, and cannot easily find it on the web, but a file with the name !, sorts before . and .. on Linux. Now why you would want such an awful file name ...
Fangjun Jiang
Fangjun Jiang 2011년 11월 14일
Using a=a(~[a.isdir]) should solve the problem!
Walter Roberson
Walter Roberson 2011년 11월 14일
dir() is *not* defined to return . and .. first in any of the operating systems. dir() is defined to return the order that the operating system returns. In turn, the order returned by the operating system can depend upon the file system type -- some filesystems sort directories contents automatically and some do not (and some use tree structures.)

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

Image Analyst
Image Analyst 2011년 11월 14일

0 개 추천

Can't you specify a file extension to dir() and get only those specific files and not any directories?

카테고리

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

질문:

2011년 11월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by