我将一些数据从sql数据库下载到6000 x 5的单元格中。我希望将2:end列转换为datenum。在四列中,数据库中的某些日期为NULL(请参阅下面的示例,了解我正在下载的数据)。我希望这些NULL日期为NaN或零。
'100364MM' 'null' '1997-06-01 00:00:00.0' '1997-06-01 00:00:00.0' 'null'
'100377MM' 'null' '1997-06-01 00:00:00.0' '1997-06-01 00:00:00.0' 'null'
'100394MM' 'null' '1997-06-01 00:00:00.0' '1997-06-01 00:00:00.0' 'null'
'100395MM' 'null' '1997-06-01 00:00:00.0' '1997-06-01 00:00:00.0' 'null'
'100996MM' 'null' '1997-07-01 00:00:00.0' '1997-07-01 00:00:00.0' 'null'
'101005MM' 'null' '1997-07-01 00:00:00.0' '1997-07-01 00:00:00.0' 'null'
我一直在玩下面的行但是我收到一条错误消息,说DATENUM失败了。我猜这是由于' null'?
my_dates = zeros(6000, 5)
my_dates = cellfun(@datenum, data_matrix(:, 2:end));
答案 0 :(得分:2)
问题是datenum
一找到不合格的字符串就会退出并出错。因此,在您的情况下以矢量化的方式进行操作很困难(参见Divakar's answer,艰难)。
这是一个使用try
和for
循环的快速而肮脏的解决方案:
首先定义数据:
d = {'100364MM' 'null' '1997-06-01 00:00:00.0' '1997-06-01 00:00:00.0' 'null'
'100377MM' 'null' '1997-06-01 00:00:00.0' '1997-06-01 00:00:00.0' 'null'
'100394MM' 'null' '1997-06-01 00:00:00.0' '1997-06-01 00:00:00.0' 'null'
'100395MM' 'null' '1997-06-01 00:00:00.0' '1997-06-01 00:00:00.0' 'null'
'100996MM' 'null' '1997-07-01 00:00:00.0' '1997-07-01 00:00:00.0' 'null'
'101005MM' 'null' '1997-07-01 00:00:00.0' '1997-07-01 00:00:00.0' 'null'};
然后:
result = NaN(size(d));
for n = 1:numel(d)
try
result(n) = datenum(d(n));
end
end
这给出了
result =
NaN NaN 729542 729542 NaN
NaN NaN 729542 729542 NaN
NaN NaN 729542 729542 NaN
NaN NaN 729542 729542 NaN
NaN NaN 729572 729572 NaN
NaN NaN 729572 729572 NaN
答案 1 :(得分:2)
矢量化方法 -
%// a1 is the input cell array
a1= {
'100364MM' 'null' '1997-06-01 00:00:00.0' '1997-06-01 00:00:00.0' 'null'
'100377MM' 'null' '1997-06-01 00:00:00.0' '1997-06-01 00:00:00.0' 'null'
'100394MM' 'null' '1997-06-01 00:00:00.0' '1997-06-01 00:00:00.0' 'null'
'100395MM' 'null' '1997-06-01 00:00:00.0' '1997-06-01 00:00:00.0' 'null'
'100996MM' 'null' '1997-07-01 00:00:00.0' '1997-07-01 00:00:00.0' 'null'
'101005MM' 'null' '1997-07-01 00:00:00.0' '1997-07-01 00:00:00.0' 'null'}
ind1 = ~strcmp(a1,'null')
a1(~ind1)={NaN}
ind1(:,1)=false
%// Choose one of the following two, as the output format demands
a1(ind1) = num2cell(datenum(a1(ind1))) %// datenum as cells of double
a1(ind1) = cellstr(num2str(datenum(a1(ind1)))) %// datenum as cells of strings
输出 -
选项1:
a1 =
'100364MM' [NaN] [729542] [729542] [NaN]
'100377MM' [NaN] [729542] [729542] [NaN]
'100394MM' [NaN] [729542] [729542] [NaN]
'100395MM' [NaN] [729542] [729542] [NaN]
'100996MM' [NaN] [729572] [729572] [NaN]
'101005MM' [NaN] [729572] [729572] [NaN]
选项2:
a1 =
'100364MM' [NaN] '729542' '729542' [NaN]
'100377MM' [NaN] '729542' '729542' [NaN]
'100394MM' [NaN] '729542' '729542' [NaN]
'100395MM' [NaN] '729542' '729542' [NaN]
'100996MM' [NaN] '729572' '729572' [NaN]
'101005MM' [NaN] '729572' '729572' [NaN]