设置i = 1:文件结束

时间:2014-06-04 19:05:01

标签: matlab for-loop

我正在尝试将MATLAB读入文件列表中。但是,我有几个这些文件的列表,它们都是不同的长度。如何设置for循环以使i从1到文件末尾? (I'我所说的是&= 39;对于i = 1:END OF FILE' part。

为了说明,我的.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

我的代码如下所示:

for y = 1:9
flist = fopen([num2str(year(y))'_MDA8_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
    for i = 1:END OF FILE % Number of files CHECK EACH TIME FILE IS MODIFIED
        fname = fgetl(flist); % Reads next line of list, which is the name of the next data file
        disp(fname); % Stores name as string in fname
        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{i} = site_data;
    % Stuff
    end
end

1 个答案:

答案 0 :(得分:0)

试试这段代码:

for y = 1:9

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

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

        fid = fopen(newFileName);
        while ~feof(fid)
            currentLine = fgetl(fid);
            % Do stuff on each sub-file
        end 
        fclose(fid);
    end
    flcose(flist);
end

还有另一个检查,你可以这样做:

flist = fopen([num2str(year(y))'_MDA8_mat.dat']);
line = fgetl(flist);

while(ischar(line))
   %Do process (like open next file with the line variable)

   %read the next line
   line = fgetl(flist);
end