How to find the index of the line to find the occurrence of perticular character

5 views (last 30 days)
Hi
I have multiple text files, and am reading. But I want to find the number lines in a text files(s), and also find the occurrence of dotted line(s)'------'. I use the following code, but I am unable to find the index of the dotted lines(s).My code giving 'Zero'. In fact the first dotted line occurs at line number 23 (row number), and the second dotted lines is occurring at line number 41 (row number). Please help me how can I do this.
clc;clear all;
clc
tic
FileList=dir('D:\Mekala_Backupdata\Matlab2010\Filesfolder\Try2/');
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
% fid = fopen('yourFile.ext');
for j=1:size(FileName,2)
fid=fopen(['D:\Mekala_Backupdata\Matlab2010\Filesfolder\Try2/',FileName{j}],'r'); %%opening each files and read each line
allText = textscan(fid,'%s','delimiter','\n');
numberOfLines = length(allText{1});
fclose(fid)
end
[~,indx]=ismember('----+',FileName,'rows');

Accepted Answer

Image Analyst
Image Analyst on 9 Jan 2016
Try this:
filename = 'RainFallReport1.txt'
fid = fopen(filename);
thisLine = fgetl(fid);
linesRead = 1;
dashedLineIndexes = [];
while ischar(thisLine)
disp(thisLine) % Comment out to not echo to command window.
if ~isempty(strfind(thisLine, '-----------------------'))
dashedLineIndexes = [dashedLineIndexes, linesRead];
end
linesRead = linesRead + 1;
thisLine = fgetl(fid);
end
fclose(fid);
fprintf('\n\nTotal number of lines in file "%s" = %d.\n', filename, linesRead);
fprintf('Dashes found on line(s):\n');
fprintf(' %d.\n', dashedLineIndexes);
  2 Comments
Mekala balaji
Mekala balaji on 14 Jan 2016
Sir/Madam
The number of lines between two dotted lines will vary in each text file, but I only want some common data. Read one file, and extract the required data, and then read second files, then extract required data. The required data and their names are same. Due to the different number of lines between dotted line, I can read all text files once, and save matrix form, and extract required data at once. Please kindly help. As attached herewith two files, after reading each files only extract
RainFallID & LMD Name(above the first dotted line), and 10 Under Pipe 1.Response Value (only close value), Sample.Measuring timequality (only close value) given between two dotted lines.
Many many thanks in advance
Image Analyst
Image Analyst on 14 Jan 2016
I don't know what you want. But as you read the lines you can use things like textscan() and sscanf() to parse it. So just pull out what you need and save it in variables with those functions.

Sign in to comment.

More Answers (0)

Categories

Find more on Large Files and Big Data 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!