Plot HH:MM on x-axis with time data imported from excel

Hi everyone,
I have tried several times and looked up in the questions but still can't find a solution that works for me.
I have time data in excel from 9:00 to 16:50 with time step of 5 minutes (9:00, 9:05, 9:10,...16:50) I want to have the time on the x-axis of my plots in the HH:MM format. When I import the time values in Matlab with xlsread the time is converted to decimal numbers. If i plot them directly I have decimal numbers on the x-axis so I tried datenum, datestr etc but nothing worked so far. In addition, I would like to select specific ticks to appear on the x-axis (9:00, 10:00, 11:30, 13:00, 14:30, 16:50).
Any help is appreciated, I have my master thesis to handle in in a few weeks and I want the plots to be perfect :D
Patrick

4 commentaires

Guillaume
Guillaume le 26 Fév 2020
Modifié(e) : Guillaume le 26 Fév 2020
What version of matlab are you using? It must be fairly old if you're still using xlsread and datenum
I am using R2017b.
I just managed to have the HH:MM format on the x-axis but not the ticks I want. I did it this way:
x = 9/24:1/288:17/24-2/288;
plot(x,Tent1_SO2_Monday)
datetick('x','HH:MM PM')
Now I see on my graph the ticks every hour (9:00 AM, 10:00 AM... 5:00 PM).
Can you help in selecting my specific ticks to show on the plot ? (9:00 AM, 10:00 AM, 11:30 AM, 1:00 PM, 2:30 PM and 4:50 PM)
Thanks,
Patrick
I should have ask that initially as well, can you attach a sample xls file?
In fact since I have always the same timestep and the beginning and end of the days are also always the same I created the time vector as 'x'.
Anyway, in the xls file the first columns is the date and time as recorded by the device, the second column is the time I entered manually because I couldn't use/import the values from the 1st column with Matlab.
Thanks,
Patrick

Connectez-vous pour commenter.

Réponses (1)

I was asking for an example of the original file in order to show you how to import it properly.
Anyway, a variation of this should work:
data = readtable('yourexcelfile.xlsx'); %readtable is much better than xlsread
data.DateTime_GMT_01_00 = datetime(data.DateTime_GMT_01_00, 'InputFormat', 'MM/dd/yy hh:mm:ss a');
plot(data.DateTime_GMT_01_00, data.nameof2ndvariable);
xtickformat('hh:mm a'); %Note that 'HH:MM PM' is not a valid format

3 commentaires

Thank you very much! Yes it works indeed :)
Patrick
What if I want to have several days (5 days from 13.01.2020 to 17.01.2020) on the x-axis each day starting at 9:00 AM and ending at 4:50 PM without interruptions between 4:50 PM to 9:00 AM of the next day?
Thanks,
Patrick
Hi, I tried this on my data, but it says "Error using datetime (line 589). Numeric input data must be a matrix with three or six columns, or else three, six, ..."
Could you please tell me what is not correct with my data or input?

Connectez-vous pour commenter.

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by