How to convert cell array of characters to a datetime array without looping through each cell individually?
12 次查看(过去 30 天)
显示 更早的评论
I have a very large cell array (32001x1) for which a very small portion looks like this
' " 8 / 2 4 / 2 0 1 8 8 : 5 9 : 1 8 A M " '
' " 8 / 2 4 / 2 0 1 8 9 : 0 4 : 1 8 A M " '
' " 8 / 2 4 / 2 0 1 8 9 : 0 9 : 1 8 A M " '
' " 8 / 2 4 / 2 0 1 8 1 : 0 4 : 1 8 P M " '
' " 1 0 / 4 / 2 0 1 8 1 : 2 4 : 1 8 P M " '
I want to convert this cell array into a datetime array to get the following
'24-Aug-2018 08:59:18'
'24-Aug-2018 09:04:18'
'24-Aug-2018 09:09:18'
'24-Aug-2018 13:04:18'
'04-Oct-2018 13:24:18'
Currently, I am using a for loop that iterates through each cell array, but that takes a long time considering the size of my cell array (32001x1). I want to figure out a faster way to perform the conversion by eliminating the need to loop through each element of the cell array and perhaps perform something once to change every cell of the array at once. I am also using MATLAB R2017a. Any ideas on how I can achieve this?
Below, I've created a sample code that uses the very small portion of the cell array as mentioned above with the method I am using.
% Create sample cell array of "dates"
cellDates = {' " 8 / 2 4 / 2 0 1 8 8 : 5 9 : 1 8 A M " ';...
' " 8 / 2 4 / 2 0 1 8 9 : 0 4 : 1 8 A M " ';...
' " 8 / 2 4 / 2 0 1 8 9 : 0 9 : 1 8 A M " ';...
' " 8 / 2 4 / 2 0 1 8 1 : 0 4 : 1 8 P M " ';...
' " 1 0 / 4 / 2 0 1 8 1 : 2 4 : 1 8 P M " '};
% Convert cellDates to a table
date_time = cell2table(cellDates);
% Iterate through each cell
for i = 1:(size(date_time,1))
% Get each value once at a time from "date_time" table
tempVar = date_time.cellDates(i); % Extracts each individual cell
% Convert from cell to string
tempVar = tempVar{1}; % Extracts character string from cell
% Split that string to separate date from time (creates 1x3 cell array)
tempVar = strsplit(tempVar,' ');
% Get string with data
dateVal = tempVar{1};
% Determine weird characters " and space (they are the 1st and 2nd
% characters in dateVal)
bad = dateVal(1);
bad1 = dateVal(2);
% Remove unecessary characters
dateVal = dateVal(dateVal~=bad); % Removing spaces
dateVal = dateVal(dateVal~=bad1); % Removing quotations
% Repeat
timeVal = tempVar{2};
timeVal = timeVal(timeVal~=bad);
timeVal = timeVal(timeVal~=bad1);
% Repeat
amPM = tempVar{3};
amPM = amPM(amPM~=bad);
amPM = amPM(amPM~=bad1);
% Convert to datetime value
dateTime(i,1) = datetime([dateVal ' ' timeVal ' ' amPM], 'InputFormat', 'M/d/yyyy h:m:s a');
end
0 个评论
采纳的回答
Sean de Wolski
2019-5-31
编辑:Sean de Wolski
2019-5-31
Converting the cellstr to a string makes this much easier:
cellDates = {' " 8 / 2 4 / 2 0 1 8 8 : 5 9 : 1 8 A M " ';...
' " 8 / 2 4 / 2 0 1 8 9 : 0 4 : 1 8 A M " ';...
' " 8 / 2 4 / 2 0 1 8 9 : 0 9 : 1 8 A M " ';...
' " 8 / 2 4 / 2 0 1 8 1 : 0 4 : 1 8 P M " ';...
' " 1 0 / 4 / 2 0 1 8 1 : 2 4 : 1 8 P M " '};
strDates = string(cellDates); % convert to string
strDates = erase(strDates, ["""", " "]); % remove spaces, " to save on typing
datetime(strDates, 'InputFormat', 'M/d/yyyyh:mm:ssa') % Convert
5×1 datetime array
24-Aug-2018 08:59:18
24-Aug-2018 09:04:18
24-Aug-2018 09:09:18
24-Aug-2018 13:04:18
04-Oct-2018 13:24:18
2 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!