我有一组数据,第1列显示日期(以MATLAB序列号形式(,第2列显示降雨量,如下所示:
x = [ 22029 81
23040 90.2
26701 90.2
28223 188
36973 92
38386 99.8
40580 117.8];
到目前为止,我的代码是:
out = x(x(:,2) > 180,:,1); %gets all the dates with rainfall greater than 180
out2 = x(x(:,2) > 80,:,1); %gets all the dates with rainfall greater than 80
c1 = out(:,1);
c2 = out2(:,1);
rain = datestr(c1); %converts from MATLAB serial number to date
rain2 = datestr(c2); %converts from MATLAB serial number to date
X = sprintf('Heaviest rainfall will occur on %s.',rain);
X2 = sprintf('Heavy rainfall will occur on %s.',rain2);
disp(X)
disp(X2)
我的disp(X)
工作并显示:
Heaviest rainfall will occur on 08-Apr-0077.
但是,我的disp(X2)
不工作并且显示
Heavy rainfall will occur on 22002003968447-------AJFAMFFpaepaeernbrrbb-------0000000000011166770010337151.
如何修复disp(X2)
的代码,使其显示:
Heavy rainfall will occur on 23-Apr-0060, 29-Jan-0063, 06-Feb-0073, 08-Apr-0077, 24-Mar-0101, 04-Feb-0105, 04-Feb-0105.
之所以会发生这种情况,是因为矩阵在MATLAB中以列主顺序存储。在使用sprintf
函数之前,您可以对Rain2
字符数组进行重新排序,如下所示:
rain2(:,end+1) = ','; rain2(end) = '.'; %For , in between and . at the end
rain2 = rain2.'; %Reordering
X2 = sprintf('Heavy rainfall will occur on %s', rain2);
结果:
>> X2
X2 =
'Heavy rainfall will occur on 23-Apr-0060,29-Jan-0063,06-Feb-0073,08-Apr-0077,24-Mar-0101,04-Feb-0105,07-Feb-0111.'