Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

process a list of files with a specific extension name in matlab

Tags:

matlab

How can I process all the files with ".xyz" extension in a folder? The basic idea is that I want a list of file names and then a for loop to load each file.

like image 664
dalibocai Avatar asked Sep 02 '11 18:09

dalibocai


People also ask

How do I get the file extension in MATLAB?

Description. [ filepath , name , ext ] = fileparts( filename ) returns the path name, file name, and extension for the specified file. fileparts only parses the specified filename .

How do I list files in a directory in MATLAB?

To search for multiple files, use wildcards in the file name. For example, dir *. txt lists all files with a txt extension in the current folder. To search through folders and subfolders on the path recursively, use wildcards in the path name.

What file extension is used for script files in MATLAB?

A script file is an external file that contains a sequence of MATLAB statements. Script files have a filename extension . m and are often called M-files. M-files can be scripts that simply execute a series of MATLAB statements, or they can be functions that can accept arguments and can produce one or more outputs.


1 Answers

As others have already mentioned, you should use the DIR function to list files in a directory.

If you are still looking, here is an example to show how to use the function:

dirName = 'C:\path\to\folder';              %# folder path
files = dir( fullfile(dirName,'*.xyz') );   %# list all *.xyz files
files = {files.name}';                      %'# file names

data = cell(numel(files),1);                %# store file contents
for i=1:numel(files)
    fname = fullfile(dirName,files{i});     %# full path to file
    data{i} = myLoadFunction(fname);        %# load file
end

Of course, you would have to supply the function that actually reads and parses the XYZ files.

like image 99
Amro Avatar answered Sep 17 '22 16:09

Amro