Convert cell to duration array

35 Ansichten (letzte 30 Tage)
Corentin OGER
Corentin OGER am 21 Mai 2019
Bearbeitet: Corentin OGER am 22 Mai 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

Akzeptierte Antwort

Stephen23
Stephen23 am 21 Mai 2019
Bearbeitet: Stephen23 am 21 Mai 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 Kommentar
Corentin OGER
Corentin OGER am 21 Mai 2019
Bearbeitet: Corentin OGER am 21 Mai 2019
Great, thanks Stephen!
It works just like cell2mat for my case!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

convert_to_metric
convert_to_metric am 21 Mai 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 Kommentare
Corentin OGER
Corentin OGER am 21 Mai 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 am 22 Mai 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'

Melden Sie sich an, um zu kommentieren.


Corentin OGER
Corentin OGER am 22 Mai 2019
Bearbeitet: Corentin OGER am 22 Mai 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'

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Produkte


Version

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by