How to find the index of the line to find the occurrence of perticular character
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Mekala balaji
el 9 de En. de 2016
Comentada: Image Analyst
el 14 de En. de 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 comentarios
Respuesta aceptada
Image Analyst
el 9 de En. de 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 comentarios
Image Analyst
el 14 de En. de 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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Text Files en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!