Help converting cell to matrix of doubles (Large matrix)

1 Ansicht (letzte 30 Tage)
nori lam
nori lam am 18 Okt. 2016
Beantwortet: nori lam am 19 Okt. 2016
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

Akzeptierte Antwort

Matthew Eicholtz
Matthew Eicholtz am 18 Okt. 2016
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

Weitere Antworten (1)

nori lam
nori lam am 19 Okt. 2016
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.

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by