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

조회 수: 12 (최근 30일)
Jorge Zambrano
Jorge Zambrano 2018년 7월 30일
댓글: Venkatapathi 2021년 1월 12일
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],'.-');
  댓글 수: 2
Jorge Zambrano
Jorge Zambrano 2018년 8월 2일
This method worked beautifully. Thank you Benjamin.
Venkatapathi
Venkatapathi 2021년 1월 12일
To understand this code, what concepts should i learn from MATLAB?

댓글을 달려면 로그인하십시오.

카테고리

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