Read multiple text files and extract part of data by name
Show older comments
Hi,
I have used the below code to read and extract selected data from text file, I used textscan (to read) and
find(~cellfun(@isempty,regexpi(allText,'RainFallID')))
to identify the required data by name. It working well, but If I run 10,000 text files it become dam slow, takes more than three hours. Please kindly help some if there is any faster way.
Sinerely,
clc;
clear all;
clc
tic
FileList=dir('D:\Mekala_Backupdata\Matlab2010\Filesfolder\PartofTextFilesData/');
j=1;
for i=3:1:(size(FileList)) %%read all files from folder of specified dir
FileName{j}=FileList(i).name;
j=j+1;
end
for j=1:size(FileName,2)
fid=fopen(['D:\Mekala_Backupdata\Matlab2010\Filesfolder\PartofTextFilesData/',FileName{j}],'r'); %%opening each files and read each line
allText = textscan(fid,'%s','delimiter','\n');
numberOfLines = length(allText{1});
allText=allText{:};
for k=1:size(allText,1)
idx_RainFallID=find(~cellfun(@isempty,regexpi(allText,'RainFallID')));
idx_LMDName=find(~cellfun(@isempty,regexpi(allText,'LMD Name')));
idx_10Under1=find(~cellfun(@isempty,regexpi(allText,'10 Under Pipe 1.Response Value')));
idx_RainFallIDtemp=allText(idx_RainFallID);
idx_RainFallIDtemp2=regexp(idx_RainFallIDtemp,' +','split');
b(j,1)=str2double(idx_RainFallIDtemp2{1}{1,3});
Variable{1,1}=char(idx_RainFallIDtemp2{1}{1,1});
end
fclose(fid)
end
2 Comments
Walter Roberson
on 23 Jan 2016
You pull out idx_LMDName and idx_10Under1 but you do not do anything with them?
You always write over Variable{1,1} instead of storing for each file?
Is it correct that your desired output is the list of filenames, and a vector of the numeric forms of the corresponding RainfallID ?
Kanakaiah Jakkula
on 23 Jan 2016
Accepted Answer
More Answers (0)
Categories
Find more on Text Data Preparation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!