Import and plot file with day and time information

I have a file and i cannot manage to plot my data correctly. I can plot values as datapoints but i need also the time.. Anyone who can help?

1 Comment

An actual file instead of a picture of it would be a lot more useful for us to test.
i cannot manage to plot my data correctly. To tell what you did wrong we need to know what you did.

Sign in to comment.

 Accepted Answer

I'm not sure what you're doing wrong since you haven't shown us what you're doing. The following may be what you're after:
t = readtable('data3.csv');
t.datetime = t.(1) + t.(2); %merge date and time into one variable
plot(t.datetime, t{:, 3:5});

7 Comments

t.datetime = t{:, 1} + duration(t{:, 2});
readtable has been greatly improved since 2016a to detect dates and times properly. The problem is that in your version, it doesn't automatically detect that column 1 is date and column 2 is time (in R2018b it does, I think the improvement was finalised in R2018a).
I would suspect that in your version, both t.(1) and t.(2) are cell arrays of character vectors (or possibly 2D char arrays, can you confirm?), you'll want to convert that to a datetime array. This may work:
t.datetime = datetime(t.(1)) + duration(t.(2));
If it doesn't you'll probably have to help datetime and/or duration in parsing the text by specifying the 'InputFormat'. Maybe:
t.datetime = datetime(t.(1), 'InputFormat', 'dd/MM/yyyy') + ...
duration(t.(2), 'InputFormat', 'hh:mm:ss')
Error using duration (line 237) Input data must be a numeric matrix with three columns, or three separate numeric arrays.
You need to help us help you here and try to work out what the problem is yourself, otherwise it gets a bit frustrating.
It looks like duration doesn't accept string input in your version (edit: just checked, it doesn't). This should work:
t.datetime = datetime(strcat(t.(1), {' '}, t.(2)));
or
t.datetime = datetime(strcat(t.(1), {' '}, t.(2)), 'InputFormat', 'dd//MM/yyyy hh:mm:ss');
It can be argued which one is the wrong plotted data.
In your first figure, there is no concept of times, all points are plotted as if there was a fixed time interval between them. In the second figure, points are plotted according to time. As it turns out many points have the exact same time, so they're all at the same x position.
E.g. samples 1 to 23 have the exact same time of 15:12:21, so they're all at the same x = 15:12:21 in your second plot, but at indices 1 to 23 in your first plot.
If they're not meant to be at the same time, then the time resolution embedded in your text file is not fine enough.
Well, then it is not meant to be the same time! Yes, some samples are at the second, but the fraction of second would be different. Unfortunately, that information is not embedded in your text file. The best would be to fix whatever created these text files so that it writes the sub-second information.
There are many ways you could fix this depending on how robust you want it to be. If we assume that all the samples in one text file are all continuous and acquire at a rate of 125 Hz then a simple way to solve the problem is to cumulatively add 1/125 second to the timing of the 1st sample (thus completely ignoring the embedded timing of the remaining samples). If that is acceptable, then:
t.datetime = datetime(strcat(t{1, 1}, {' '}, t{1, 2}) + ... %timing of 1st sample
seconds(1/125) * (0:height(t)-1)'; %cumulatively add 1/125 second to timing of each sample
Well, most likely the error you actually received was Invalid Expression as I forgot a closing bracket. You must have added the bracket in the wrong place.
t.datetime = datetime(strcat(t{1, 1}, {' '}, t{1, 2})) + ... %timing of 1st sample
seconds(1/125) * (0:height(t)-1)'; %cumulatively add 1/125 second to timing of each sample
Or since you don't really care about the actual acquisition time of the samples, you could simply ignore the time information from the text file and do:
plot(seconds(1/125) * (0:height(t)-1), t{:, 3:5})

Sign in to comment.

More Answers (0)

Categories

Asked:

on 7 Nov 2018

Edited:

on 6 Jan 2025

Community Treasure Hunt

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

Start Hunting!