RMS错误,AME,文本文件中的相关性以及在excel中导出夏季统计信息

亲爱的专家

我试图做一个代码,能够计算RMS,AME, 24文本文件的名称output_00.txt到文件夹中的ouput_23.txt的关联 。 读完每个文件后,我在每个文本文件中将样本的30% (总样本为323)作为-9999.0,然后使用散点数据插值 ,插入这些-9999.0值并通过RMS误差,AME,相关。

更新2

S = dir('interpot_linear_*.txt'); N = sort({S.name}); for K = 1 : length(N) infile = N{K}; whichfile = sscanf(infile, 'interpot_linear_%c%c'); % Load the data data = load(infile); % separate the data columns, just to make the code clear Lat = data(:,1); % Column 1 is Latitude Lon = data(:,2); % Column 2 is Longitude Tmp = data(:,3); % Column 3 is Temperature % Creating 30% of sample as -9999.0 n=size(data,1); %n=323; nr=round(n*0.3); idx30=randperm(n,nr); Tmp(idx30)=-9999.0; % Find the "good" data points good_temp = find(Tmp > -9999.000); % find the "bad" data points bad_temp = find(Tmp == -9999.000); % creating vector T = scatteredInterpolant(Lat(good_temp), Lon(good_temp), Tmp(good_temp), 'linear'); % use the interpolation object to interpolate temperature values interp_values = T(Lat(bad_temp), Lon(bad_temp)); % replace the bad values with the interpolated values Tmp(bad_temp) = interp_values; %recall oringinal temperature column Tmp_original=data(:,3); % Here calculate RMS error RMS=sqrt(sum((Tmp_original(:)-Tmp(:)).^2)/n); % Here calculate mean absolute error MAE=sum(abs(Tmp_original(:)-Tmp(:)))/n; % Here calculate correlation coefficient correl=corr(Tmp_original(:),Tmp(:)); %here i am spliting R^2 as two different terms M_org= mean(Tmp_original); % mean of original temperature data. M_ext= mean(Tmp); % mean of extimated temperature data. std_org=std(Tmp_original); % standard deviation of orginal temp std_ext=std(Tmp); % standard deviation of extimated temp. total=length(Tmp_original); M1=(Tmp_original(:)- M_org); M2=(Tmp(:)- M_ext); M1=M1'; % Here i am creating numminator of the formula Nu=M1*M2; %giveing sum (M1*M2) % Here i am creating dinominator of the formula div=total*std_org*std_ext; %R ^2 R=(Nu/div)^2; rep(K,1)=RMS; rep(K,2)=MAE; rep(K,3)=correl; rep(K,4)=R; rep(K,5)=str2num(whichfile); end xlswrite('statistics',rep); 

我想在excel / text文件中包含这四个列,即第一列是文件号,第二个没有RMS错误,第三列AME和这个文件的第四列相关值,这些24 txt文件的汇总统计

我的一个文本文件看起来像这样

 21.500 60.500 295.867 21.500 61.500 295.828 21.500 62.500 295.828 21.500 63.500 295.867 21.500 64.500 296.102 21.500 65.500 296.234 21.500 66.500 296.352 21.500 67.500 296.336 21.500 68.500 296.305 21.500 69.500 298.281 21.500 70.500 301.828 21.500 71.500 302.094 21.500 72.500 299.469 21.500 73.500 301.711 21.500 74.500 302.317 21.500 75.500 302.757 21.500 76.500 303.030 21.500 77.500 303.137 21.500 78.500 303.078 22.500 60.500 295.477 22.500 61.500 295.484 22.500 62.500 295.516 22.500 63.500 295.547 22.500 64.500 295.852 22.500 65.500 295.859 22.500 66.500 295.852 22.500 67.500 295.711 22.500 68.500 295.969 22.500 69.500 298.562 22.500 70.500 300.828 22.500 71.500 302.352 22.500 72.500 300.570 22.500 73.500 301.383 22.500 74.500 301.311 22.500 75.500 301.381 22.500 76.500 301.692 22.500 77.500 301.837 22.500 78.500 301.814 23.500 60.500 294.906 23.500 61.500 294.898 23.500 62.500 295.000 23.500 63.500 295.078 23.500 64.500 295.297 23.500 65.500 295.359 23.500 66.500 295.297 23.500 67.500 295.312 23.500 68.500 296.664 23.500 69.500 298.781 23.500 70.500 299.211 23.500 71.500 300.109 23.500 72.500 301.000 23.500 73.500 301.594 23.500 74.500 302.000 23.500 75.500 300.911 23.500 76.500 300.520 23.500 77.500 300.702 23.500 78.500 300.718 24.500 60.500 294.578 24.500 61.500 294.516 24.500 62.500 294.734 24.500 63.500 294.789 24.500 64.500 294.844 24.500 65.500 294.562 24.500 66.500 294.734 24.500 67.500 296.367 24.500 68.500 297.438 24.500 69.500 298.531 24.500 70.500 298.453 24.500 71.500 299.195 24.500 72.500 300.062 24.500 73.500 300.351 24.500 74.500 301.055 24.500 75.500 300.958 24.500 76.500 300.512 24.500 77.500 299.734 24.500 78.500 299.787 25.500 60.500 296.258 25.500 61.500 296.391 25.500 62.500 296.672 25.500 63.500 296.398 25.500 64.500 295.773 25.500 65.500 295.812 25.500 66.500 296.609 25.500 67.500 297.977 25.500 68.500 297.109 25.500 69.500 297.828 25.500 70.500 298.430 25.500 71.500 298.836 25.500 72.500 298.703 25.500 73.500 300.207 25.500 74.500 300.110 25.500 75.500 300.013 25.500 76.500 299.917 25.500 77.500 299.470 25.500 78.500 299.023 26.500 60.500 294.484 26.500 61.500 298.266 26.500 62.500 296.773 26.500 63.500 296.892 26.500 64.500 297.012 26.500 65.500 297.131 26.500 66.500 297.250 26.500 67.500 296.188 26.500 68.500 295.938 26.500 69.500 296.906 26.500 70.500 297.828 26.500 71.500 299.312 26.500 72.500 299.359 26.500 73.500 299.262 26.500 74.500 299.165 26.500 75.500 299.069 26.500 76.500 298.972 26.500 77.500 298.875 26.500 78.500 296.773 27.500 60.500 292.710 27.500 61.500 295.880 27.500 62.500 294.643 27.500 63.500 295.710 27.500 64.500 296.362 27.500 65.500 296.333 27.500 66.500 296.452 27.500 67.500 295.352 27.500 68.500 295.148 27.500 69.500 295.750 27.500 70.500 295.750 27.500 71.500 296.070 27.500 72.500 295.227 27.500 73.500 297.534 27.500 74.500 297.437 27.500 75.500 297.340 27.500 76.500 297.870 27.500 77.500 297.405 27.500 78.500 296.609 28.500 60.500 290.935 28.500 61.500 293.494 28.500 62.500 292.513 28.500 63.500 293.580 28.500 64.500 294.647 28.500 65.500 295.715 28.500 66.500 295.951 28.500 67.500 295.773 28.500 68.500 295.375 28.500 69.500 295.438 28.500 70.500 294.664 28.500 71.500 294.906 28.500 72.500 294.812 28.500 73.500 295.805 28.500 74.500 296.335 28.500 75.500 296.864 28.500 76.500 296.731 28.500 77.500 294.557 28.500 78.500 294.696 29.500 60.500 289.161 29.500 61.500 291.108 29.500 62.500 290.383 29.500 63.500 291.450 29.500 64.500 292.517 29.500 65.500 293.585 29.500 66.500 294.652 29.500 67.500 295.719 29.500 68.500 296.797 29.500 69.500 293.375 29.500 70.500 294.305 29.500 71.500 294.070 29.500 72.500 293.750 29.500 73.500 295.539 29.500 74.500 295.859 29.500 75.500 296.057 29.500 76.500 292.532 29.500 77.500 292.799 29.500 78.500 292.924 30.500 60.500 287.387 30.500 61.500 288.722 30.500 62.500 288.253 30.500 63.500 289.320 30.500 64.500 290.387 30.500 65.500 291.455 30.500 66.500 292.522 30.500 67.500 292.884 30.500 68.500 294.198 30.500 69.500 295.394 30.500 70.500 293.320 30.500 71.500 292.930 30.500 72.500 293.570 30.500 73.500 294.648 30.500 74.500 295.383 30.500 75.500 290.535 30.500 76.500 290.929 30.500 77.500 291.182 30.500 78.500 291.294 31.500 60.500 285.613 31.500 61.500 286.336 31.500 62.500 286.123 31.500 63.500 287.190 31.500 64.500 288.257 31.500 65.500 289.325 31.500 66.500 289.239 31.500 67.500 290.049 31.500 68.500 291.364 31.500 69.500 292.678 31.500 70.500 293.992 31.500 71.500 293.422 31.500 72.500 294.438 31.500 73.500 294.141 31.500 74.500 288.564 31.500 75.500 289.087 31.500 76.500 289.468 31.500 77.500 289.707 31.500 78.500 289.805 32.500 60.500 283.839 32.500 61.500 283.950 32.500 62.500 283.993 32.500 63.500 285.060 32.500 64.500 286.127 32.500 65.500 286.272 32.500 66.500 285.726 32.500 67.500 287.214 32.500 68.500 288.529 32.500 69.500 289.843 32.500 70.500 291.847 32.500 71.500 294.312 32.500 72.500 294.812 32.500 73.500 286.621 32.500 74.500 287.271 32.500 75.500 287.781 32.500 76.500 288.148 32.500 77.500 288.374 32.500 78.500 288.458 33.500 60.500 282.065 33.500 61.500 281.564 33.500 62.500 281.863 33.500 63.500 282.930 33.500 64.500 283.306 33.500 65.500 282.759 33.500 66.500 282.405 33.500 67.500 284.380 33.500 68.500 285.694 33.500 69.500 287.238 33.500 70.500 289.703 33.500 71.500 291.025 33.500 72.500 284.704 33.500 73.500 285.483 33.500 74.500 286.120 33.500 75.500 286.616 33.500 76.500 286.970 33.500 77.500 287.182 33.500 78.500 287.253 34.500 60.500 280.290 34.500 61.500 279.178 34.500 62.500 279.733 34.500 63.500 280.339 34.500 64.500 279.792 34.500 65.500 279.246 34.500 66.500 279.278 34.500 67.500 281.545 34.500 68.500 282.859 34.500 69.500 285.093 34.500 70.500 287.237 34.500 71.500 282.814 34.500 72.500 283.722 34.500 73.500 284.487 34.500 74.500 285.111 34.500 75.500 285.593 34.500 76.500 285.933 34.500 77.500 286.132 34.500 78.500 286.189 35.500 60.500 278.516 35.500 61.500 276.792 35.500 62.500 277.373 35.500 63.500 276.826 35.500 64.500 276.279 35.500 65.500 275.732 35.500 66.500 276.150 35.500 67.500 278.710 35.500 68.500 280.484 35.500 69.500 283.450 35.500 70.500 280.952 35.500 71.500 281.987 35.500 72.500 282.881 35.500 73.500 283.633 35.500 74.500 284.243 35.500 75.500 284.712 35.500 76.500 285.039 35.500 77.500 285.224 35.500 78.500 285.267 36.500 60.500 276.742 36.500 61.500 274.406 36.500 62.500 273.859 36.500 63.500 273.313 36.500 64.500 272.766 36.500 65.500 272.219 36.500 66.500 273.023 36.500 67.500 275.875 36.500 68.500 279.662 36.500 69.500 279.117 36.500 70.500 280.280 36.500 71.500 281.302 36.500 72.500 282.182 36.500 73.500 282.920 36.500 74.500 283.517 36.500 75.500 283.972 36.500 76.500 284.285 36.500 77.500 284.457 36.500 78.500 284.487 37.500 60.500 277.406 37.500 61.500 277.547 37.500 62.500 276.375 37.500 63.500 275.484 37.500 64.500 276.820 37.500 65.500 275.312 37.500 66.500 274.875 37.500 67.500 275.875 37.500 68.500 277.308 37.500 69.500 278.600 37.500 70.500 279.750 37.500 71.500 280.758 37.500 72.500 281.624 37.500 73.500 282.349 37.500 74.500 282.932 37.500 75.500 283.374 37.500 76.500 283.674 37.500 77.500 283.832 37.500 78.500 283.849 

你的代码有几个问题:

  1. 如果你的文件名是output_00.txt (例如,它们以小写字母“o”开始),那么你必须将你的第一行代码改为S = dir('output_*.txt'); 。 Matlab是区分大小写的。 如果您的文件名以小写字母“o”开头,则Matlab将永远不会find名称以“Output”(大写“O”)开头的文件。 这可能会解决data的错误。 相同的文件名问题将影响sscanf() ,所以在sscanf()也必须使用小写的“o”。
  2. RMSE公式是不正确的:你不把这两个项目平方,然后减去它们,你必须反过来; 也就是先减后再上电。 正确的公式是RMS=sqrt(sum((Tmp_original(:)-Tmp(:)).^2)/n);
  3. variablesRMScorrelMAE是标量,所以用(:)运算符来表示variables是无意义的,
  4. rep被声明为24×3的matrix,但是随后的行rep=[RMS(:);MAE(:);correl(:)]; 你让rep一个3×1向量。
  5. 在最后的for循环中有几个问题: xlmwrite()的第一个参数(例如Statistics )必须是一个string,而不是一个variables(除非这个variables包含文件名作为string)。 而第二个参数必须用rep代替。
  6. TmpTmp_original都是列向量,所以做(:)运算符是毫无意义的。 往上看。

由于您主要关心的是保存到磁盘的问题,所以我将专注于此。 我的build议是build立rep作为一个matrix,然后保存这样的matrix之外for循环,一旦你的所有文件已被扫描和处理。 因为你的代码现在是不是很清楚你是否想要把代码作为一个matrix并且一次保存到磁盘上,或者你希望把rep作为一个向量并且以逐个文件的forms保存到磁盘上。

  1. 首先要做的是移动rep=zeros(24,3); 一直到for循环开始之前。 如果你还想附加文件ID,那么rep实际上应该是一个24×4的matrix。
  2. 更改rep=[RMS(:);MAE(:);correl(:)]; rep(k,1)=RMS; rep(k,2)=MAE; rep(k,3)=correl; rep(k,1)=RMS; rep(k,2)=MAE; rep(k,3)=correl; 。 通过这种方式,你可以在第k行第1列上写RMS ,然后在第k行第2列上写MAE ,同样的故事correl 。 随着k去,这将填补matrixrep 。 如果您还想附加文件ID,请记住, whichfile是一个string,您不能将string和数字连接到同一个matrix。 即时的select是将whichfile转换为数字,这要归功于str2num()但要记住,这样的转换将删除前导零(Matlab不允许前导零),所以文件ID 00将是0 ,文件01将是1等等。 所以你可能要添加rep(k,4)=str2num(whichfile); 这也将填补rep第四列。
  3. 删除内部for循环,其中包含xlmwrite() 。 我们不会为每个文件写一行,而是以批处理模式写出整个matrix。
  4. 在主for循环之后,将xlmwrite()如下所示: xlmwrite('myFinalFile',rep); 如果一切正常, myFinalFile应该包含你的matrix作为一个24×3的matrix。 你也可以用你喜欢的文件名改变myFinalFile

所以你的问题是,在一次迭代中,加载并分配给数据的文件(在你的代码的第8行)是空的,因此数据的大小是[0,0]。

我假设有一个文件是空的,这就是为什么你得到这个回应。

所以试着find这个文件,最好的方法是运行脚本,看看K的哪个值出现错误,然后导航到文本文件,看它是否真的是空的。