Friday 3 February 2017

How to read or open more than one files in a folder using MATLAB

files = dir('E:\myfolder\*txt'); 

This command will read all files with extension txt

c_files = struct2cell(files);

The files will be saved as structure array. It can't be used for further use. So we are converting it into cell.

filenames=c_files(1,:)';
This command will make all filenames in a column.

L = length(filenames);

This code will store the total number of files into the variable L.

Now you can open each files in a folder using a for loop. The filename (eg: myfile.txt) will be stored in the with complete directory link (eg:- E:\myfolder\myfile.txt) will be stored in the variable filename. you can open it using fopen function as shown below.
for g = 1:L;

A = 'E:\myfolder\';B = filenames{g};filename = strcat([A,B]);
fid = fopen(filename,'r'); 
end

Full code:-


files = dir('E:\myfolder\*txt'); 
c_files = struct2cell(files);filenames=c_files(1,:)'; 
L = length(filenames); 
for g = 1:L;
A = 'E:\myfolder\';
B = filenames{g};
filename = strcat([A,B]);
fid = fopen(filename,'r'); 
end




Keyword: Mutliple files, matlab, opening, more than one file, in a folder, MATLAB, how to


No comments :

Post a Comment

Related Posts Plugin for WordPress, Blogger...