通读并保存具有不同文件名的文件

时间:2014-06-04 14:53:38

标签: matlab for-loop save

我有一个CSV文件列表。这些文件的文件名已存储在[year' _MDA8_mat.dat']表格中。我想在每个文件中读入MATLAB并保存输出。如何编写代码以便依次考虑每年,并且每年会保存输出.mat文件?

这是我在其中一个文件中阅读的内容:

flist = fopen('2006_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:27299 % Number of files 

        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;
        % Do some more stuff
end
save ('2006_MDA8_1990_2014.mat', '-v7.3')

我试着像这样写一个for循环:

year = 2006:2014
for y = 1:9
    flist = fopen([year(y) '_MDA8_mat.dat']);
    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:1500 % Number of files 
           % Same as above   
        end
    end
    save ([year '_MDA8_1990_2014.mat'], '-v7.3')
end

然而,当我运行它时,它不会像对一个文件脚本那样做同样的事情。我不太确定错误发生在哪里,但是MATLAB告诉我feof出现错误,这似乎没有用。

1 个答案:

答案 0 :(得分:0)

组合数字和字符串时,您需要在数字上执行num2str

[num2str(year) '_MDA8_1990_2014.mat']