Convert cell to duration array

36 views (last 30 days)
Corentin OGER
Corentin OGER on 21 May 2019
Edited: Corentin OGER on 22 May 2019
Hi,
I had a program that was using time stored as seconds (double), I'm converting it to "duration" datatype, so the plots look nicer, natively displayed as HH:MM:SS.
However, I need to extract duration data stored in portions of a cell array to make a duration array.
%Simplified example with time as double:
TimingCell = {0; 63; 122} %doubles in a cell (0min, 1min03, 2min02)
MyArray = cell2mat(TimingCell) % Works : array of doubles
Now that I have converted to duration:
%Simplified example with time as duration:
TimingCell = {duration(0,0,0); duration(0,1,3); duration(0,2,2)} %durations in a cell (0min, 1min03, 2min02)
MyArray = cell2mat(TimingCell) % Does not work (wanted result: array of durations)
I get:
Error using cell2mat (line 52)
CELL2MAT does not support cell arrays containing cell arrays or objects.
I would like to get a duration array, I want something like the array created by this line:
MyArray = duration([0;0;0], [0;1;2], [0;3;1])
To summarize my question:
Is there an elegant equivalent to "cell2mat" when cell content is "duration"?
Thanks in advance for your time,
Corentin

Accepted Answer

Stephen23
Stephen23 on 21 May 2019
Edited: Stephen23 on 21 May 2019
"Is there an elegant equivalent to "cell2mat" when cell content is "duration"?"
Yes, this is really easy with a comma-separated list and concatenation, which does much the same thing as cell2mat, possibly with reshape if required.
>> TimingCell = {duration(0,0,0); duration(0,1,3); duration(0,2,2)}
TimingCell =
[00:00:00]
[00:01:03]
[00:02:02]
>> MyArray = vertcat(TimingCell{:})
MyArray =
00:00:00
00:01:03
00:02:02
  1 Comment
Corentin OGER
Corentin OGER on 21 May 2019
Edited: Corentin OGER on 21 May 2019
Great, thanks Stephen!
It works just like cell2mat for my case!

Sign in to comment.

More Answers (2)

convert_to_metric
convert_to_metric on 21 May 2019
Hi Corentin,
Since all of your data can be stored as either an array of doubles or an array of durations, you can probably skip using cell arrays altoghether. This would turn your second example into a one liner by converting the curly brackets into square brackets:
TimingCell = [duration(0,0,0); duration(0,1,3); duration(0,2,2)] %durations in aan array (0min, 1min03, 2min02)
Or if your original timing data is already stored as a cell array with each cell containg a double representing seconds: convert that cell array to a matrix using cell2mat, before making use of the duration function with the help of the seconds function. The example below uses the format property to have the output look the way you want it. Note that the output of the seconds function is already a duration, which may be sufficient for your needs.
TimingCell = {0; 63; 122} %doubles in a cell (0min, 1min03, 2min02)
MyArray = cell2mat(TimingCell) % Works : array of doubles
MyArray2 = duration(seconds(MyArray),'format','hh:mm:ss')
  2 Comments
Corentin OGER
Corentin OGER on 21 May 2019
Hi c2m, and thank you for your reply,
In fact, only a part of my cell contains duration data, the rest contains strings, laid out like this:
TimingCell =
5×3 cell array
[00:00:00] [00:00:02] 'Starting Phase'
[00:00:05] [00:00:20] 'Acceleration'
[00:00:20] [00:02:30] 'Sustained speed'
[00:02:30] [00:02:45] 'Braking'
[00:02:45] [00:02:47] 'Stopping'
(this derives from a GUI table which the user can edit manually)
In the original version of my program, the first two colums were doubles, I used it as a database containing all timing information. I used cell2mat to retrieve only one column with cell2mat. Since I cannot retrieve a whole column at once, I'm probably going to use one table only with duration datatype, and keep a cell only for text descriptions.
Corentin OGER
Corentin OGER on 22 May 2019
In the end I converted to using a "table" to store my data, it's more ceonvenient than a cell for my use (it means that I have to rewrite a lot of code since several function access ths data but it looks cleaner)
DonneesTimings =
Start Stop Description
________ ________ _________________
00:00:00 00:00:02 'Starting Phase'
00:00:05 00:00:20 'Acceleration'
00:00:20 00:02:30 'Sustained speed'
00:02:30 00:02:45 'Braking'
00:02:45 00:02:47 'Stopping'

Sign in to comment.


Corentin OGER
Corentin OGER on 22 May 2019
Edited: Corentin OGER on 22 May 2019
In case someone faces the same issue, I'll update what I really did in the end.
I used the "table" type of structure, which accepts mixed datatypes (like cells) but is easier to access directly. Each column is like a field in a struct, the difference being that they must all have the same number of rows. It's convenient to delete a complete row at once.
DonneesTimings =
Start Stop Description
________ ________ _________________
00:00:00 00:00:02 'Starting Phase'
00:00:05 00:00:20 'Acceleration'
00:00:20 00:02:30 'Sustained speed'
00:02:30 00:02:45 'Braking'
00:02:45 00:02:47 'Stopping'

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Products


Release

R2016b

Community Treasure Hunt

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

Start Hunting!