Reputation: 35
I have a folder with multiple textfiles (labelled xxxxxds.text where x's are numbers) with data in it. The data in the text file is arranged in 3 columns and the first line of a text file consists of headers, one for each column. What I want to do is, to open the folder and read the text file # 1 say and do some processing like delete the first line and store the rest of data in three separate variables (one for each columns), then move to the second file and do the same thing and keep doing it until the end file is reached.
I am not having too much success with it. Here is my (failed) attempt at it:
dir_folder ='D:\datat_folder';
files = dir(dir_folder);
files = files(arrayfun(@(x) ~strcmp(x.name(1),'.'),files)); % Remove hidden files
dir_length=length(files);
for steps=1:dir_length % for loop
point=[files,num2str(steps),'*.txt']
[A,B,C] = textread(point,'%f %f %f' , 2);
end
I would be grateful for suggestions if anyone has any.
Cheers.
Upvotes: 0
Views: 140
Reputation: 1675
To get just the filenames that you want, you can be more specific with your dir
command:
dir_folder ='D:\datat_folder\'; %note the '\' at the end
fnames = dir([dir_folder '*.text');
Then, loop over each file, as you were doing, but alter your textread
command a little bit:
for Ifile = 1:length(fnames)
fname = fnames(Ifile).name;
%load the text data while using the 'headerlines' option to ignore the first line of data
[A,B,C]=textread(fname,'%f %f %f','headerlines',1);
%do your processing here
% blah blah blah
end
Upvotes: 1