Ignore first 5 lines

25 visualizaciones (últimos 30 días)
Onur Esmeray
Onur Esmeray el 27 de Abr. de 2021
Comentada: Image Analyst el 28 de Abr. de 2021
path= 'my directory here';
[file,path] = uigetfile('*.cmn');
if isequal(file,0)
disp('User selected Cancel');
else
disp(['User selected ', fullfile(path,file)]);
end
selectedfile = fullfile(path,file);
contents=fileread(selectedfile);
Here is my code to start with, i want to ignore first 5 lines when reading the file. How can i do that?
  6 comentarios
Onur Esmeray
Onur Esmeray el 27 de Abr. de 2021
Editada: Onur Esmeray el 27 de Abr. de 2021
Somehow i coulnd't get both of them to work (cause i'm a noob :p) so i did it like this and it took me a while to.. But now i get the values i need.
Thank you very much for your information, they guided me very well
path= 'insert directory here';
[file,path] = uigetfile('*.cmn');
if isequal(file,0)
disp('User selected Cancel');
else
disp(['User selected ', fullfile(path,file)]);
end
filename = fullfile(path,file);
[fname, message] = fopen(filename,'r');
if fname < 0
errordlg(strcat('Error opening--',filename,' :-',message))
err = -1;
return
end
for i =1:5 % skip headerlines (5)
line = fgetl(fname)
end
cdata = textscan(fname, '%f %f %f %f %f %f %f %f %f %f'); % reading the text file and extracting the data in a cell
rawdata=cell2mat(cdata)
Image Analyst
Image Analyst el 28 de Abr. de 2021
The cmn file will need to be zipped up for you to attach it because it's not one of the allowed filenames.

Iniciar sesión para comentar.

Respuesta aceptada

Image Analyst
Image Analyst el 27 de Abr. de 2021
You can read and ignore the first 5 lines:
% Open the file for reading in text mode.
fileID = fopen(fullFileName, 'rt');
% Read and ignore the first 5 lines of the file.
linesToIgnore = 5;
for k = 1 : linesToIgnore
textLine = fgetl(fileID);
end
% Read remaining lines.
lineCounter = 0;
while ischar(textLine)
% Read the next line.
textLine = fgetl(fileID);
% Print out what line we're operating on.
fprintf('%s\n', textLine);
lineCounter = lineCounter + 1;
end
% All done reading all lines, so close the file.
fclose(fileID);

Más respuestas (1)

Adam Danz
Adam Danz el 27 de Abr. de 2021
If the data is organized in rows, then you should use a file reading function designed for tabular data: readtable | readcell | readmatrix | readtimetable
Then, use indexing to remove the first few lines.
T(1:5,:) = []; % removes first 5 rows of data

Categorías

Más información sobre Data Import and Export en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by