How can i convert time format to do a plot?

조회 수: 1 (최근 30일)
Marco Silva
Marco Silva 2019년 5월 20일
편집: Josh 2019년 5월 21일
hi guys, my problem is:
I need to do a plot with this two vectors:
timestamp =
1×6 cell array
{'00:20.360'} {'00:39.132'} {'00:40.360'} {'00:49.259'} {'00:59.508'} {'111:59:11.360'}
// =========
values =
100 200 300 400 500 600
so, i cant plot the timestamp with that format.
I need to convert the timestamp to a correct format to plot.
the timestamp represents a time format, for example, 111:59:11.360 = 111Hours, 59 minutes, 11 seconds and 360 millisecond.
I already try the function datetime(timestamp,'InputFormat','HH:mm:ss.SSS') but the array doesn't have the same format for all the values and didn't exist the format ''HHH:mm:ss.SSS' for the value '111:59:11.360'.
Can you help me, please? I need to convert the timestamp to the correct time value for, after this, do the plot(timestamp, values);
thank you guys,

답변 (1개)

Josh
Josh 2019년 5월 21일
편집: Josh 2019년 5월 21일
You should be able to use regular expressions to extract the hour, minute, second, and ms values from the time stamps and then convert the extracted values to seconds:
% Create cell array containing time stampes
timestamps = {'00:20.360','00:39.132','00:40.360','00:49.259','00:59.508','111:59:11.360'};
% Extract time stamp parts; hour, minute, second, and fraction will be stored in a structure array
parse = regexp(timestamps, ...
'(?<hour>\d+)?:?(?<minute>\d+):(?<second>\d+)\.(?<fraction>\d+)', 'names', 'once');
parse = cat(1, parse{:});
% Convert the structure array into a regular matrix with hours in the first row, minutes
% in the second row, seconds in the third row, and ms in the fourth row:
parts = str2double(struct2cell(parse));
% Use matrix multiplication to calculate the total value (in seconds) of each timestamp
in_secs = [3600, 60, 1, 0.001] * parts;
% Then plot the data:
plot(in_secs, values);

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by