找到MATLAB正在处理的.dat文件的哪一行

时间:2014-06-05 19:53:12

标签: matlab file-io

我有一个MATLAB脚本,它从文本文件中读取一行。文本文件的每一行都包含CSV的文件名。我需要跟踪MATLAB正在处理的行,以便我可以在单元阵列中保存该行的数据。我怎么能这样做?

为了说明,我的.dat文件的前几行如下所示:

2006-01-003-0010.mat
2006-01-027-0001.mat
2006-01-033-1002.mat
2006-01-051-0001.mat
2006-01-055-0011.mat
2006-01-069-0004.mat
2006-01-073-0023.mat
2006-01-073-1003.mat
2006-01-073-1005.mat
2006-01-073-1009.mat
2006-01-073-1010.mat
2006-01-073-2006.mat
2006-01-073-5002.mat
2006-01-073-5003.mat

我需要将每个site_data文件中的变量.mat保存到O3_data的不同单元格中。因此,我需要一个计数器,以便O3_data{1}是来自文本文件第一行的数据,O3_data{2}是来自第二行的数据等。

这段代码有效,但是没有使用计数器就完成了,所以我只得到我正在读的其中一个文件的数据:

year = 2006:2014;
for y = 1:9
    flist = fopen(['MDA8_' num2str(year(y)) '_mat.dat']); % Open the list of file names - CSV files of states with data under consideration
    nt = 0; % Counter will go up one for each file loaded

    while ~feof(flist) % While end of file has not been reached
        fname = fgetl(flist);
        disp(fname); % Stores name as string in fname
        fid = fopen(fname);

        while ~feof(fid)
            currentLine = fgetl(fid);    
            load (fname, 'site_data'); % Load current file. It is all the data for one site for one year
            O3_data = site_data;
            % Do other stuff
        end
        fclose(fid);
    end
    fclose(flist);
end

如果我添加time index部分,MATLAB告诉我Subscript indices must either be real positive integers or logicals. nt是一个整数,所以我不知道我做错了什么。我需要time index以便我可以拥有O3_data {i},其中每个i是我正在阅读的文件之一。

year = 2006:2014;
for y = 1:9
    flist = fopen(['MDA8_O3_' num2str(year(y)) '_mat.dat']); % Open the list of file names - CSV files of states with data under consideration
    nt = 0; 

    while ~feof(flist) % While end of file has not been reached
        fname = fgetl(flist);
        fid = fopen(fname);

        while ~feof(fid)
            currentLine = fgetl(fid);
            nt = nt+1; % Time index
            load (fname, 'site_data'); % Load current file. It is all the data for one site for one year
            O3_data{nt} = site_data;
            % Do other stuff
        end  
        fclose(fid);
    end
    fclose(flist);
end

2 个答案:

答案 0 :(得分:0)

请尝试以下操作 - 请注意,由于存在外部for循环,因此必须在该循环之外初始化nt变量,以便我们不会覆盖前几年的数据(或之前的 j 。我们可以避免内部while循环,因为刚读取的文件是* .mat文件,我们使用load命令将其单个变量加载到工作区中。

year = 2006:2014;
nt   = 0;

data_03 = {};   % EDIT added this line to initialize to empty cell array
                % note also the renaming from 03_data to data_03

for y = 1:9
    % Open the list of file names - CSV files of states with data under 
    % consideration
    flist = fopen(['MDA8_O3_' num2str(year(y)) '_mat.dat']); 

    % make sure that the file identifier is valid
    if flist>0

        % While end of file has not been reached
        while ~feof(flist) 
            % get the name of the *.mat file
            fname = fgetl(flist);

            % load the data into a temp structure
            data = load(fname,'site_data');

            % save the data to the cell array
            nt = nt + 1;
            data_03{nt} = data.site_data;
        end

        fclose(flist);  % EDIT moved this in to the if statement
    end

end

请注意,上面假设每个* .dat文件都包含一个* .mat文件列表,如上例所示。

请注意上一个帖子中上述代码中的EDIT。

答案 1 :(得分:0)

尝试以下方法:

years = 2006:2014;
for y=1:numel(years)
    % read list of filenames for this year (as a cell array of strings)
    fid = fopen(sprintf('MDA8_O3_%d_mat.dat',years(y)), 'rt');
    fnames = textscan(fid, '%s');
    fnames = fnames{1};
    fclose(fid);

    % load data from each MAT-file
    O3_data = cell(numel(fnames),1);
    for i=1:numel(fnames)
        S = load(fnames{i}, 'site_data');
        O3_data{i} = S.site_data;
    end

    % do something with O3_data cell array ...
end