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

14 vues (au cours des 30 derniers jours)
Jorge Zambrano
Jorge Zambrano le 30 Juil 2018
Commenté : Venkatapathi le 12 Jan 2021
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.

Réponses (1)

Benjamin Kraus
Benjamin Kraus le 30 Juil 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],'.-');
  2 commentaires
Jorge Zambrano
Jorge Zambrano le 2 Août 2018
This method worked beautifully. Thank you Benjamin.
Venkatapathi
Venkatapathi le 12 Jan 2021
To understand this code, what concepts should i learn from MATLAB?

Connectez-vous pour commenter.

Produits


Version

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by