How do I plot time (hh:mm:ss.000) vs samples as imported from a .csv file?

11 views (last 30 days)
Please see the attached .csv file. I am trying to read time (hh:mm:ss.000) and plot it vs Sample 1 or Sample 2. 000 are milliseconds. The issue I have is that when I read time with the import tool, it gets read as a constant variable. How do I read time and plot it with the file formatting vs my samples? If the import tool can't read time in this format, how do I import time with the specified formatting as a variable to plot against my samples? I am using Matlab version R2016a.
Thank you for your time.

Answers (1)

Benjamin Kraus
Benjamin Kraus on 30 Jul 2018
If you upgrade to R2018a, this process is a little more straightforward, but this code will work in R2016a.
Step 1: Import your data and update the variable names:
t = readtable('Test.csv','ReadVariableNames',true);
t.Properties.VariableNames{1} = 'Time';
Step 2: Convert the first column to duration. I cheat a little and use datetime to do the conversions. Note that this step is done automatically in R2018a, so you can skip it. In addition, in R2018a, you can do the conversion by calling duration directly, instead of using datetime.
t.Time= datetime(t.Time,'InputFormat','HH:mm:ss.SSS')-datetime('today');
t.Time.Format = 'hh:mm:ss.SSS';
Step 3: Plot
plot(t.Time, [t.Sample1 t.Sample2],'.-');

Products


Release

R2016a

Community Treasure Hunt

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

Start Hunting!