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

12 次查看(过去 30 天)
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.

回答(1 个)

Benjamin Kraus
Benjamin Kraus 2018-7-30
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],'.-');

类别

Help CenterFile Exchange 中查找有关 Grid Lines, Tick Values, and Labels 的更多信息

产品


版本

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by