Reputation: 3980
I am attempting to read some data from a .cnv file. I can open the file with:
TopFolder = 'Name_of_my_file';
SubFolder = dir(fullfile(TopFolder,'*.cnv'));
fid = fopen(fullfile(TopFolder,SubFolder{i}));
All of the data is located following the string END, which is on a separate line to the other headers. I would like to import the data that is stored on the lines following this string. How can this be achieved?
For example, the a section of the .cnv file is as follows:
# datcnv_in = D:\data\110606_000.hex D:\instrument software\Seabird 2010-07\Seabird Con Files\SBE19_2108_ScufaTOBS.con
# datcnv_skipover = 0
# file_type = ascii
*END*
-0.051 0.0312 15.4328 138.1551 0.0000 0.0000 0.0000 0.0000 0.000e+00
-0.033 0.0305 15.4277 138.1551 0.0000 0.0000 0.0000 0.0000 0.000e+00
So, I would like to avoid those lines prior to End
Maybe a first step would be to find the line number of END? How would I do this?
Upvotes: 0
Views: 2371
Reputation: 31040
First of all open the file and search through the lines until you find 'END'
fid = fopen('yourfile.cnv') % open file to read
fseek(fid,0,-1); % set read position to beginning of file
while strcmp(fgetl(fid),'*END*') == 0 end % go through lines until '*END*'
Next read data, line by line, into a matrix (data
):
n=1;
while 1
tline = fgetl(fid) % read in line
if ~ischar(tline), break, end % if eof, break and finish
data(:,n) = sscanf(tline,'%f') % put numbers in a matrix (in columns)
n=n+1
end
fclose(fid) % close file
Upvotes: 1
Reputation: 9313
Try this:
L = '';
while isempty(findstr(L,'*END*')) % if the line doesn't contain *END* => read a new line
L = fgetl(fid); % read next line of file
end
a = fscanf(fid,'%g %g %g %g'); % add as many %g as columns in your data
fclose(fid);
I've added some comments on how this works. Basically, this reads the opened file line by line until it finds a line containing *END*
.
You could use strcmp(L,'*END*')
if there is more than 1 line that may contain the same characters.
A warning: this code assumes that the file you are reading contains at least one line that has the *END*
characters, if not, you'll get an error when attempting to read beyond the EOF.
Hope this helps.
Upvotes: 0
Reputation: 21563
When using the import wizard in matlab you can specify the number of header rows. If you set this to four, it should work in this example.
After using the import wizard you can let it generate code if you want to automate the process for the future.
Upvotes: 0