Preventing Matlab from truncating timestamp when importing from excel -
i importing excel file of timestamps matlab. seems whenever have timestamp @ turn of midnight i.e. '13/5/2015 12:00 pm', matlab read '13/5/2015' only.
excel samples '13/5/2015 12:00 pm' '13/5/2015 12:01 pm'
imported matlab using [na1, na2, raw] = xlsread('excel.xls');
raw = { '13/5/2015'; '13/5/2015 12:01 pm'}
this prevents me using datenum of same formatin. how go preventing matlab truncating timestamp when import excel?
you add in time finding entries don't end in letter , add 12:00 am
@ end of string. can used datenum
. that, can use regular expressions , search cells don't have letters @ end, each of these cells, add 12:00 am
@ end.
something this:
ind = ~cellfun(@isempty, regexp(raw, '\d*$')); strings = raw(ind); padded_strings = cellfun(@(x) [x ' 12:00 am'], strings, 'uni', 0); raw(ind) = padded_strings;
the first line of code bit obfuscated, easy explain. let's @ nested command first:
regexp(raw, '\d*$')
regexp
matlab's regular expression command. regular expressions seek find patterns in strings. here specify string, or cell array of strings , goal of regexp
find locations of pattern matched within each string. in case, i'm doing finding strings ended in numbers. if don't find such pattern, result empty. therefore, if provide cell array of strings, cell array output each element tells indices of pattern found.
as such, if obtain index string, means ended in numbers , if it's empty, ended in numbers. use cellfun
iterate through regexp
result , return logical
vector determines whether each cell array came empty or had in it. however, want opposite, why take inverse (~
). output can use slice raw
cell array , pick out strings ended numbers @ end.
that slicing done in second line of code. once out these strings, run cellfun
(third line) call appends 12:00 am
string @ end of string , set uni=0
flag output cell array , no longer simple numeric/logical array. once have output cell array third line, use indices first line of code slice original cell array , place these padded strings inside.
once run above code on example cell array, this:
raw = '13/5/2015 12:00 am' '13/5/2015 12:00 pm'
Comments
Post a Comment