How to find the index of the line to find the occurrence of perticular character
4 views (last 30 days)
Show older comments
Mekala balaji
on 9 Jan 2016
Commented: Image Analyst
on 14 Jan 2016
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');
0 Comments
Accepted Answer
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
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.
More Answers (0)
See Also
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!