Help converting cell to matrix of doubles (Large matrix)

1 次查看(过去 30 天)
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