필터 지우기
필터 지우기

Datenum Problem

조회 수: 5 (최근 30일)
Lu
Lu 2011년 5월 15일
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
bym
bym 2011년 5월 15일
can you provide a few lines of data.txt?
Lu
Lu 2011년 5월 15일
Hi proecsm,
Thank you for the reply! My data set looks a bit like this:
Hourtime Name Price1 Price2 Price3 and so on...
19.10.09 09:00:16 BASF 30 31 34
19.10.09 09:00:17 BASF 30 31 34
19.10.09 09:00:18 BASF 30 31 34
19.10.09 09:00:19 BASF 30 31 34
Is there anyway I could transform the Hourtime into numeric value? because apparently Hourtime is not loading as a string since it appears without the usual '' like '19.10.09 09:00:19'. Is there anyway i can do that because datetimenumber=datenum([dateandtime{:}],'dd.mm.yy HH:MM:SS');
is not working :(
Thank you! :)

댓글을 달려면 로그인하십시오.

채택된 답변

Oleg Komarov
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')
  댓글 수: 2
Lu
Lu 2011년 5월 15일
Dear Oleg,
Thank you for the reply! It is true, it was not consistent because I wrote the sample data set the way I get it from matlab after doing textscan, but i was already ignoring some columns.
I have one main problem: since i want to create my time intervals, i wanted to convert the datetime column (this is the format of how it looks in my file 'dd.mm.yy HH:MM:SS') into a numeric value, but whenever I type
dateandnumber=datenum([dateandtime{:}],'dd.mm.yy HH:MM:SS');
i get just one output : 734065.375185185
and the problem is I need all the numeric values for all my dateandtime values.
Is there anything I can do to fix it?
Thank you so much! :)
Lourdes
Lu
Lu 2011년 5월 15일
Dear Oleg,
Thank you so much!! This is really helpful and it works!!
Have a nice week!
Lourdes :)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Andrei Bobrov
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)))]
  댓글 수: 1
Lu
Lu 2011년 5월 15일
Dear Andrei,
Thank you for the help! I don´t entirely get your code but I guess it is trying to eliminate empty spaces? but I don´t have empty spaces in my data set. Could you please explain me a bit more the last two lines of code please? I would really appreciate it :)
Thank you,
Lourdes

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Time Series Objects에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by