How to convert time format to plot a .csv file?

5 views (last 30 days)
Hello everyone,
I am an Electronic Engineer. I'm currently working on a project in which I need to tune a PID controller and I want to plot a generated .csv file in MATLAB to do it. The SIMATIC software has a curve recorder that I could use to do it, but it is really small in size and I would like to have much more precision; also, it would be very valuable for me to learn how to do this here in MATLAB.
I have used MATLAB in a basic level and I am currently reading about the instructions I need to do this plot, but I am posting this question here to see if you can give me some pointers in avdance. What I have already seen is that it can't plot the time in the format hh:mm:ss with "real" values on the other axis, so I need to convert the time to—I think—serial date number or some other format. I've seen the instructions to do this, but the thing is that I have a lot of values in the .csv file and haven't found how to select, dissect or separate them to do the conversion of all of them. I am attaching a screenshot of the file opened in Excel. I only need to plot the time in the x-axis and the Manipulated Variable and the Process Variable in the y-axis; I have done basic plots, but for this one I need a bit of specialized, focused help.
Thanks in advance.

Answers (1)

Walter Roberson
Walter Roberson on 5 Jun 2016
[num, txt] = xlsread('Yourfile.csv');
datetimes = strjoin(txt(11:end,1), {' '}, txt(11:end,2));
time_num = datenum(datetimes);
c1 = num(1:end,1);
plot(time_num, c1)
datetick('x', 'mm:ss')
If you have R2014b or later then I recommend that you use datetime objects instead of serial date numbers:
[num, txt] = xlsread('Yourfile.csv');
datetimes = strjoin(txt(11:end,1), {' '}, txt(11:end,2));
time_obj = datetime(datetimes, 'Format', 'mm:ss');
c1 = num(1:end,1);
plot(time_obj, c1)
  1 Comment
Carlos Castillo
Carlos Castillo on 6 Jun 2016
Edited: Carlos Castillo on 6 Jun 2016
Hello Walter, thanks a lot for your answer.
Most of those commands are new for me, let me see if I get it straight.
[num, txt] = xlsread('myfile.csv');
There I'll be saving the numerical values in one array (num) and the texts in another one (txt). I see that it's possible to get the raw values in another array, also, but for my purpose that's not necessary.
datetimes=strjoin(txt(11:end,1), {' '}, txt(11:end,2));
Will this be joining every string in the first column from row 11 to the final one with a space delimiter? And the same for the second column? It's the first time I see this syntax. What does the {' '} mean? Is it the delimiter between the two strings (dates and times), or is it the delimiter between every former string? In the documentation I only find a more simple syntax for this command, and actually when I try it on the command window it gives me an error "too many input arguments." Reading the explanations for the next command I kind of see what is needed from this one, but I'm not sure how to put it.
time_num = datenum(datetimes);
Here I have a datestring composed of the date, a space and then the time—if I understood the previous command—and I will convert it to serial date numbers, ending up with an array of only those, right?
Then
c1 = num (1:end, 1);
That will save in C1 the first column of "num" from the first row to the end. Actually, I don't need that column; I'll be working with the 2nd and 3rd ones, I will change the coe accordingly.
plot(time_num, c1);
I've done that before.
datetick('x', 'mm:ss')
Here I specify the axis in 'x' and then set the format for the labels, right? This will "dispose" the date and the hour part of the time for the labels in the plot, right?
Now, for using the datetime objects instead of serial numbers:
time_obj = datetime(datetimes, 'Format', 'mm:ss');
I read what this command does, but instead of 'Format' they use 'InputFormat'. In your example 'Format' is to specify the output format of the command, right? Because in the documentation they only specify the input format.
The next two commands are clear. I know these are a lot of questions, but some things I haven't found in the documentation, and some others I've tried and found errors.
Thanks in advance

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!