Reading CSV file with time stamped data and Plotting data with Time in the X-axis

I have a csv file with data imported from a PLC time stamped to msec. I wanted to use the following "csvread" command to read the data and plot the data with time on the x axis.
Time Data format 12:38:06;865 (HH:MM:SS;FFFF) See attached csv file
Can you please assist me to be able read the timestamp in Matlab and plot the data

 Accepted Answer

[num,txt,raw] = xlsread('Test1.csv');
time = vertcat(txt{2:end,1}) ;
gvpos = num(:,1) ;
vlvlimcm = num(:,2) ;
speedctrl = num(:,3) ;
speed = num(:,4) ;
pidout = num(:,5) ;
%
dn=datenum(time,'HH:MM:SS;FFF');
plot(dn,gvpos)
datetick('x','keepticks','keeplimits')

1 Comment

Thanks a lot, that's exactly what I was after. Is there a way to display time as HH:MM:SS.FFFF when using a cursor on the graph.

Sign in to comment.

More Answers (1)

I'm not familiar with PLC files, but in anything like a recent MATLAB, you should be able to use readtable and datetime. Something like
t = readtable('Test1.csv','Format','%{HH:mm:ss;SSS}D,%f%f%f%f')
Not sure if the file has column headers, you may need to adjust the table's variable names. It looks like you'll want to convert the datetime to a duration, because it would have been read in with the date portion set to "today". Something like
t.Time = t.Time - datetime('today');
t.Time.Format = 'hh:mm:ss.SSS'
should do that. Then plot as
plot(t.Time,t.GVPos)
Hope this helps.

Community Treasure Hunt

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

Start Hunting!