Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Matlab创建文本文件_Matlab - Fatal编程技术网

使用Matlab创建文本文件

使用Matlab创建文本文件,matlab,Matlab,我正在尝试对降水数据列表进行一些更改。 数据以以下格式保存在*.txt文件中: 50810,200301010600,0.0 50810,200301010601,0.0 50810,200301010800,0.0 50810,200301010938,0.1 50810,200301010947,0.1 50810,200301010957,0.1 我拥有的每个文件都包含1年来每分钟的降水量数据。 第一个数字只是桩号,这对于文件中的每一行都是相等的。 在我的原始文件中,每分钟有一行数据 我

我正在尝试对降水数据列表进行一些更改。 数据以以下格式保存在
*.txt
文件中:

50810,200301010600,0.0
50810,200301010601,0.0
50810,200301010800,0.0
50810,200301010938,0.1
50810,200301010947,0.1
50810,200301010957,0.1
我拥有的每个文件都包含1年来每分钟的降水量数据。 第一个数字只是桩号,这对于文件中的每一行都是相等的。 在我的原始文件中,每分钟有一行数据

我想创建一个包含以下内容的新文件:

  • 只有降水量不为零的线,所以我想删除 最后一个数字为零的所有行。我已经弄明白了
  • 如果有整整一个小时没有降水,我想把所有的都去掉 并创建一个新行,该新行显示: 50810,200301010600,0.0. 如果在6点到7点之间没有降水 是在01点01分

    clear all
    
    data = load('jan-31des_2003.txt'); %opens file with data
    fid=fopen('50810_2003','w'); %opens empty file to write
    [nrow, ncol] = size(data); %size of data
    fprintf(fid,'%5s %12s %5s \r\n','Snr','Dato - Tid','RR_01') %Header
    
    for row = 1:nrow 
    
        y = data(row,2); %year
        m = data(row,3); % month
        d = data(row,4); % date
        h = data(row,5); % hour
        M = data(row,6); % minute
        p = data(row,8); % precipitation
    
        if p > 0
         fprintf(fid,'50810,%04d%02d%02d%02d%02d,%.1f \r\n',[y,m,d,h,M,p]);
        end
        if p==0 
            HERE I NEED SOME HELP
        end
    
    end
    
    fclose(fid);
    

如果p==0,在
条件下,我所需的格式代码是什么?

假设您已经将数据导入名为infle的M*3数组,并且M可以被60整除,并且您的数据在一小时的第一分钟开始,并且您有一个数组输出文件将写入新文件:

outfile = []
while ~isempty(infile)
    block = infile(1:60,:);          % Take one hour's worth of data
    if sum (block(:,3)==0) == 60     % Check if the last column are all zero
        outfile = cat(1, outfile, [block(1,1:2), 0]);  % If so, put one line to new array
    else
        filter = block(:,3)~=0;      % Otherwise, pick out only rows that the last column is not zeros
        outfile = cat(1, outfile, block(filter,:));    % Put these rows into the new array
    end
    infile(1:60,:) = [];             % Remove already processed data from original array
end

然后将整个
输出文件
数组写入文件。

假设您已将数据导入名为infle的M*3数组,并且M可被60整除,并且数据在一小时的第一分钟开始,并且您有一个数组输出文件将写入新文件:

outfile = []
while ~isempty(infile)
    block = infile(1:60,:);          % Take one hour's worth of data
    if sum (block(:,3)==0) == 60     % Check if the last column are all zero
        outfile = cat(1, outfile, [block(1,1:2), 0]);  % If so, put one line to new array
    else
        filter = block(:,3)~=0;      % Otherwise, pick out only rows that the last column is not zeros
        outfile = cat(1, outfile, block(filter,:));    % Put these rows into the new array
    end
    infile(1:60,:) = [];             % Remove already processed data from original array
end

然后将整个
输出文件
数组写入文件。

可能重复的文件可能重复。非常感谢。请点击左边的v符号并接受答案,这样人们就会乐于继续帮助你解决问题。谢谢:)我还需要另一个案子的帮助。在我的一些数据中,原始数据集中只包括降水量大于零的次数。因此,上述逐小时检查所有值的方法将不起作用。有人知道怎么做吗?也许你应该再问一个问题?听起来这个问题需要一个不同的解决方案,这很有效。非常感谢。请点击左边的v符号并接受答案,这样人们就会乐于继续帮助你解决问题。谢谢:)我还需要另一个案子的帮助。在我的一些数据中,原始数据集中只包括降水量大于零的次数。因此,上述逐小时检查所有值的方法将不起作用。有人知道怎么做吗?也许你应该再问一个问题?听起来这个问题需要一个不同的解决方案。