Help converting cell to matrix of doubles (Large matrix)

조회 수: 1 (최근 30일)
nori lam
nori lam 2016년 10월 18일
답변: nori lam 2016년 10월 19일
Good day Matlabbers!
I have a textfile with almost a million rows that I need to format.
What I want to do is break up a string date into a double matrix that has 3 columns of [yyyy mm dd]. I dont want to make a datevec as I need the months and day.
What I have done is able to extract the date into a new matrix but does anyone have a good suggestion on how to vectorize my code so I dont have to run a loop?
Here is my code
data = {'1991-08-09' '12:15:00' 66.0 200 '(Unchecked)' 18966.0 200 '(Unchecked).calc';...
'1991-08-09' '12:30:00' 66.0 200 '(Unchecked)' 18966.0 200 '(Unchecked).calc';...
'1991-08-09' '12:45:00' 66.0 200 '(Unchecked)' 18966.0 200 '(Unchecked).calc'};
% single row
date_short = [str2num(data{1}(1:4)),str2num(data{1}(6:7)),str2num(data{1}(9:10))];
% loop to get all date rows
date_short_loop = [];
for a = 1:3
date_short_loop(a,1:3) = [str2num(data{a}(1:4)),str2num(data{a}(6:7)),str2num(data{a}(9:10))];
end
Any suggestions are greatly appreciated :) Thanks Norris

채택된 답변

Matthew Eicholtz
Matthew Eicholtz 2016년 10월 18일
I think datevec is what you want.
d = datevec(data(:,1),'yyyy-mm-dd');
d = d(:,1:3); % if you only want to keep the year, month, and day

추가 답변 (1개)

nori lam
nori lam 2016년 10월 19일
Thanks for the suggestion.
I tried the datevec and it is really slow (comparable to a for loop) which is unfortunate.
I ended up using the find and replace function in Sublime text to remove the '-' marks and just read the whole file with textread.

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by