Convert cell to duration array
35 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Corentin OGER
il 21 Mag 2019
Modificato: Corentin OGER
il 22 Mag 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
0 Commenti
Risposta accettata
Stephen23
il 21 Mag 2019
Modificato: Stephen23
il 21 Mag 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 Commento
Più risposte (2)
convert_to_metric
il 21 Mag 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 Commenti
Vedere anche
Categorie
Scopri di più su Data Type Conversion in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!