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 commentaires

Adam Danz
Adam Danz le 27 Avr 2021
In the future, please format your code using the [>] format button (see instructions). I've formatted it below.
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?
Onur Esmeray
Onur Esmeray le 27 Avr 2021
MY file format is .Cmn so i am not able to use readtable, it's more like a table after deleting first 5 rows. Data example;
cnmr, USA
15.22969 145.74310 62.41328
MJdatet Time PRN Az Ele Lat Lon Stec Vtec S4
57082.537500 12.900000 1 211.21 19.13 8.975 141.931 68.15 36.40 -99.000
57082.537847 12.908333 1 211.28 19.33 9.031 141.955 67.45 36.24 -99.000
57082.539236 12.941667 1 211.55 20.15 9.252 142.049 67.45 36.85 -99.000
57082.539583 12.950000 1 211.62 20.35 9.306 142.071 66.69 36.65 -99.000
57082.540625 12.975000 1 211.83 20.97 9.464 142.137 66.69 37.11 -99.000
Image Analyst
Image Analyst le 27 Avr 2021
@Onur Esmeray, I think my code below (in the official Answers section) should have worked. I assume you tried it. Why didn't it work? Or did you not even try it? If it doesn't work, attach your file with the paperclip icon so we can test it.
Adam Danz
Adam Danz le 27 Avr 2021
Both official answers seem feasible.
According to another answer in the forum cmn files can be read using any of the functions I listed or the approach Image Analyst mentioned. All of the functions I and Image Analyst listed allow you to specify the starting row in the file!
Read the documentation and show us what you've tried and explain why it's not working.
Onur Esmeray
Onur Esmeray le 27 Avr 2021
Modifié(e) : Onur Esmeray le 27 Avr 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 le 28 Avr 2021
The cmn file will need to be zipped up for you to attach it because it's not one of the allowed filenames.

Connectez-vous pour commenter.

 Réponse acceptée

Image Analyst
Image Analyst le 27 Avr 2021

0 votes

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);

Plus de réponses (1)

Adam Danz
Adam Danz le 27 Avr 2021

0 votes

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

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by