Convert cell to duration array
조회 수: 35 (최근 30일)
이전 댓글 표시
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
채택된 답변
Stephen23
2019년 5월 21일
편집: Stephen23
2019년 5월 21일
"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
추가 답변 (2개)
convert_to_metric
2019년 5월 21일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!