Spliting Time stamp without spaces into columns

I have a Time_stamp cell array as follows:
'AA20150760317000000'
'AA20150760317000500'
'AA20150760317001000'
'AA20150760317001500'
'AA20150760317002000'
'AA20150760317002500'
'AA20150760317003000'
'AA20150760317003500'
'AA20150760317004000'
'AA20150760317004500'
'AA20150760317005000'
I wish to convert to a number array into columns:
2015 076 03 17 00 00 00
2015 076 03 17 00 05 00
which contains Year, doy, month, day, hour, minute, seconds.
I am a total noob in matlab. Greatly appreciate any help.

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 25 Mär. 2020

0 Stimmen

cell2mat(cellfun(@(S) structfun(@str2double,S), regexp(Time_stamp, '(?<year>\d\d\d\d)(?<doy>\d\d\d)(?<month>\d\d)(?<day>\d\d)(?<hour>\d\d)(?<minute>\d\d)(?<seconds>\d\d)','names','once'), 'UniformOutput', false).').'

3 Kommentare

Amol Kishore
Amol Kishore am 25 Mär. 2020
Thanks a lot
a single line of code is a charm. However would like to learn how it does this.
Walter Roberson
Walter Roberson am 25 Mär. 2020
Bearbeitet: Walter Roberson am 25 Mär. 2020
Break it into sub expressions.
There are two aspects that need matlab experience to understand.
The simpler of them is that if you have a scalar struct and use structfun without the 'uniform', 0 option, then structfun will apply the function to each structure member, and will return the results as an array. In the case where the structure members are each character vectors, then structfun @str2double is going to convert each structure member into the number it represents and then return the overall vector of values.
The more difficult part is understanding regexp. regexp can be confusing even to people who have used it a long time.
regexp has several different modes of how it can return information about what it matches. When you are working with structured text it can be very useful to get it to return a struct of matched information, with structure field names as coded in the pattern of what to match. The magic for that is using the option 'names', together with patterns coded as (?<NAME>PATTERN) . When regexp sees that sequence in combination with 'names' it knows that it needs to create a structure field with the field name NAME that contains the text matched by PATTERN. So
(?<month>\d\d)
signals to create a field named month that matches exactly two digits of text.
Note well that this is text that is returned, not the numeric equivalent of the text. Matching 07 on input will get you '07' and not 7 decimal.
Then after the structure is produced, inside a cell array because your input is a cell array, the cellfun is for processing all of those cell contents, each of which is a struct, and the structfun is used to convert the text fields to numeric fields.
There is some manipulation along the way to get the values into the right orientation to stack neatly into rows of columns.
Time_stamp = {
'AA20150760317000000'
'AA20150760317000500'
'AA20150760317001000'
'AA20150760317001500'
'AA20150760317002000'
'AA20150760317002500'
'AA20150760317003000'
'AA20150760317003500'
'AA20150760317004000'
'AA20150760317004500'
'AA20150760317005000'};
tss = string(Time_stamp);
double([extractBetween(tss, 3,6), extractBetween(tss,7,9), extractBetween(tss,10,11), extractBetween(tss,12,13), extractBetween(tss,14,15), extractBetween(tss,16,17), extractBetween(tss,18,19)])
or
tss = string(Time_stamp);
cell2mat(arrayfun(@(S,E) double(extractBetween(tss,S,E)), [3 7 10 12 14 16 18], [7 9 11 13 15 17 19], 'UniformOutput', false))

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Produkte

Version

R2013b

Community Treasure Hunt

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

Start Hunting!

Translated by