How can i convert time format to do a plot?

5 views (last 30 days)
Marco Silva
Marco Silva on 20 May 2019
Edited: Josh on 21 May 2019
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,

Answers (1)

Josh
Josh on 21 May 2019
Edited: Josh on 21 May 2019
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);

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!