Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Read from all files in a directory with a MATLAB Script

38 views
Skip to first unread message

Diana

unread,
Mar 26, 2010, 2:55:21 PM3/26/10
to
Hello everyone,

I have a MATLAB script that reads some data from a text file and then does some calculations and writes to another file. I would like to run this script for several thousand files in the same directory that don't have very similar file names. I was wondering how you would go about doing something like this. Is there anything in MATLAB similar to the find command in linux, which allows you to run a command for every file in a directory?

Thank you!

Ashish Uthama

unread,
Mar 26, 2010, 2:59:56 PM3/26/10
to

Steven Lord

unread,
Mar 26, 2010, 3:03:19 PM3/26/10
to

"Diana " <dian...@yale.edu> wrote in message
news:hoivup$n41$1...@fred.mathworks.com...

See question 4.12 in the newsgroup FAQ. You will need to have some way
either to generate a list of the files you want to process or to determine,
given a file name, whether you want to process it.

--
Steve Lord
sl...@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ


us

unread,
Mar 26, 2010, 3:05:05 PM3/26/10
to
"Diana " <dian...@yale.edu> wrote in message <hoivup$n41$1...@fred.mathworks.com>...

> Hello everyone,
>
> I have a MATLAB script that reads some data from a text file and then does some calculations and writes to another file. I would like to run this script for several thousand files in the same directory that don't have very similar file names. I was wondering how you would go about doing something like this. Is there anything in MATLAB similar to the find command in linux, which allows you to run a command for every file in a directory?
>
> Thank you!

one of the solutions

d=dir('*'); % <- retrieve all names: file(s) and folder(s)
d=d(~[d.isdir]); % <- keep file name(s), only
d={d.name}.'; % <- file name(s)
nf=numel(d);
for i=1:nf
disp(sprintf('working on %5d/%5d: %s',i,nf,d{i}));
% do something, eg,
% type(d{i});
end

us

Diana

unread,
Mar 26, 2010, 3:59:20 PM3/26/10
to
"us " <u...@neurol.unizh.ch> wrote in message <hoj0h1$3c1$1...@fred.mathworks.com>...

Thank you very much!

0 new messages