Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATLAB - read files from directory?

I wish to read files from a directory and iteratively perform an operation on each file. This operation does not require altering the file.

I understand that I should use a for loop for this. Thus far I have tried:

FILES = ls('path\to\folder');

for i = 1:size(FILES, 1);
    STRU = pdbread(FILES{i});
end

The error returned here suggests to me, a novice, that listing a directory with ls() does not assign the contents to a data structure.

Secondly I tried creating a file containing on each line a path to a file, e.g.,

C:\Documents and Settings\My Documents\MATLAB\asd.pdb
C:\Documents and Settings\My Documents\MATLAB\asd.pdb

I then read this file using the following code:

fid = fopen('paths_to_files.txt');
FILES = textscan(fid, '%s');
FILES = FILES{1};
fclose(fid);

This code reads the file but creates a newline where a space exists in the pathway, i.e.

'C:\Documents'
'and'
'Setting\My'
'Documents\MATLAB\asd.pdb'

Ultimately, I then intended to use the for loop

for i = 1:size(FILES, 1)
    PDB = pdbread(char(FILES{i}));

to read each file but pdbread() throws an error proclaiming that the file is of incorrect format or does not exist.

Is this due to the newline separation of paths when the pathway file is read in?

Any help or suggestions greatly apppreciated.

Thanks, S :-)

like image 307
Darren J. Fitzpatrick Avatar asked Jun 10 '10 10:06

Darren J. Fitzpatrick


People also ask

How do I read a file from a directory in Matlab?

S = dir(...); % all of the names are in the structure S anyway. N = {S.name}; % but if you really want a cell array of names, do this.

How do I read files in a folder?

Create a file object by passing the required file path as a parameter. Read the contents of each file using Scanner or any other reader. Append the read contents into a StringBuffer. Write the StringBuffer contents into the required output file.

How do I read a filename in Matlab?

Accepted Answerfiles = dir(filePattern);


2 Answers

First Get a list of all files matching your criteria:
( in this case pdb files in C:\My Documents\MATLAB )

matfiles = dir(fullfile('C:', 'My Documents', 'MATLAB', '*.pdb'))

Then read in a file as follows:
( Here i can vary from 1 to the number of files )

data = load(matfiles(i).name)

Repeat this until you have read all your files.


A simpler alternative if you can rename your files is as follows:-

First save the reqd. files as 1.pdb, 2.pdb, 3.pdb,... etc.

Then the code to read them iteratively in Matlab is as follows:

for i = 1:n
    str = strcat('C:\My Documents\MATLAB', int2str(i),'.pdb'); 
    data = load(matfiles(i).name);

% use our logic here  
% before proceeding to the next file

end
like image 60
TheCodeArtist Avatar answered Sep 20 '22 12:09

TheCodeArtist


I copy this from yahoo answers! It worked for me

% copy-paste the following into your command window or your function

% first, you have to find the folder
folder = uigetdir; % check the help for uigetdir to see how to specify a starting path, which makes your life easier

% get the names of all files. dirListing is a struct array. 
dirListing = dir(folder);

% loop through the files and open. Note that dir also lists the directories, so you have to check for them.
for d = 1:length(dirListing)
    if ~dirListing(1).isdir
        fileName = fullfile(folder,dirListing(d).name); % use full path because the folder may not be the active path

        % open your file here 
        fopen(fileName)

        % do something

    end % if-clause
end % for-loop
like image 25
Leonardo Hermoso Avatar answered Sep 19 '22 12:09

Leonardo Hermoso