Convert cell to duration array
35 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Akzeptierte Antwort
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
Weitere Antworten (2)
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
Siehe auch
Kategorien
Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!