从.cnv文件导入数据到matlab

时间:2012-11-02 09:09:45

标签: matlab import

我正在尝试从.cnv文件中读取一些数据。我可以用以下文件打开文件:

TopFolder = 'Name_of_my_file';
SubFolder = dir(fullfile(TopFolder,'*.cnv'));
fid = fopen(fullfile(TopFolder,SubFolder{i}));

所有数据都位于字符串END之后,字符串END与其他标题分开。我想导入存储在此字符串后面的行上的数据。怎么能实现这一目标?

例如,.cnv文件的一部分如下:

# 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

所以,我想避免 End

之前的那些行

也许第一步是找到END的行号?我该怎么做?

3 个答案:

答案 0 :(得分:1)

首先打开文件并搜索一行,直到找到' 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*'

接下来逐行读取数据到矩阵(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

答案 1 :(得分:0)

在matlab中使用导入向导时,您可以指定标题行的数量。 如果将此值设置为4,则应该在此示例中起作用。

使用导入向导后,如果您希望将来的流程自动化,可以让它生成代码。

答案 2 :(得分:0)

试试这个:

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

我已经就这是如何工作添加了一些评论。基本上,这会逐行读取打开的文件,直到找到包含*END*的行。


如果有超过1行可能包含相同的字符,您可以使用strcmp(L,'*END*')


警告:此代码假定您正在阅读的文件至少包含一行*END*个字符,如果没有,您在尝试阅读超出EOF时会出错

希望这有帮助。