How can i convert time format to do a plot?

4 次查看(过去 30 天)
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 中查找有关 Data Type Conversion 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by