Datenum Problem
1 回表示 (過去 30 日間)
古いコメントを表示
Hi everyone!
I am loading a .txt file into matlab in the following way:
fid = fopen('data.txt');
data = textscan(fid,'%s %s %*f %*s %*f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %*[^\n]','HeaderLines',2,'Delimiter','\b\t','EndofLine','\r');
%disp(data{1});
fclose(fid);
%GET DATE AND TIME
dateandtime=data{1}(:);
%CONVERT DATETIME TO NUMERIC VALUE
datetimenumber=datenum([dateandtime{:}],'dd.mm.yy HH:MM:SS');
But when I load it like this, matlab brings the data set as cell arrays like this: EDU>> whos data Name Size Bytes Class Attributes
data 1x42 15544862 cell
So when I try to create a numeric value for the date and time (which has a format 'dd.mm.yy HH:MM:SS') matlab creates this variable:
EDU>> whos datetimenumber Name Size Bytes Class Attributes
datetimenumber 1x1 8 double
but this is wrong because my dateandtime cell array has a 31591x1 size, so the datenum should also create a 31591x1 numeric dates and times.
EDU>> whos dateandtime Name Size Bytes Class Attributes
dateandtime 31591x1 3032702 cell
I would really appreciate any hints of how can I fix this and create numeric dates and times for my dateandtime cell.
Have a nice week!
Lourdes
2 件のコメント
採用された回答
Oleg Komarov
2011 年 5 月 15 日
Your textscan is not consistent with the lines you posted:
textscan(fid,'%s %s %*f %*s %*f
For:
s s s f f f
19.10.09 09:00:16 BASF 30 31 etc...
Should be
data = textscan(fid, ['%s%s%s%*f' repmat('%f',1,n)],HeaderLines,2,'CollectOutput',1);
where n is the number of prices except the first one which is skipped.
No need to specify delimiter and line end (see default behaviour)
EDIT
Even better would be:
fmt = ['%17c%s%*f' repmat('%f',1,40) '%*[^\n]'];
opt = {'HeaderLines',2,'CollectOutput',1};
data = textscan(fid,fmt,opt{:});
dates = datenum(data{1},'dd.mm.yy HH:MM:SS')
その他の回答 (1 件)
Andrei Bobrov
2011 年 5 月 15 日
so?
fid = fopen('data.txt');
t = textscan(fid,'%s','Delimiter','BASF');
fid =fclose(fid);
t = t{:};
t = reshape(strtrim(t(cellfun(@(x)~isempty(x),t)))',2,[])';
out = [datenum(t(:,1),'dd.mm.yy HH:MM:SS') str2num(char(t(:,2)))]
参考
カテゴリ
Help Center および File Exchange で Time Series Objects についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!