Sanjo
Sanjo

Reputation: 175

Matlab cell array parsing

I have a large cell array and I'm trying to vectorize some string parsing. The cell is 100000 x 1 and looks like this:

data =   
'"2016-07-27T14:18:08.519Z"'  
'"2016-07-27T14:18:16.549Z"'  
'"2016-07-27T14:18:21.544Z"'  
'"2016-07-27T14:18:27.517Z"'  

I want to parse this into two cell arrays that look like this:

date_str, which would look like this:

'2016-07-27'  
'2016-07-27'  
'2016-07-27'  
'2016-07-27'  

time_str, which would look like this:

'14:18:08.519'  
'14:18:16.549'  
'14:18:21.544'  
'14:18:27.517'  

I have looked at using cellfun(@strsplit,data), but it doesn't let me specify a delimiter for the "strsplit" function.

Upvotes: 0

Views: 116

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112699

You can use regexprep to remove the double quotes, and then regexp (with the 'split' option) to split at the desired character. I'm assuming the splitting criterion is simply the occurrence of 'T'.

data = regexprep(data, '^"|"$',''); % remove double quotes
result = regexp(data, 'T', 'split'); % split at 'T'
result = vertcat(result{:}); % un-nest cell array
date_str = result(:,1);
time_str = result(:,2);

Upvotes: 1

Related Questions