How to write datetime in a plot?

I want to read a file.csv but my problem is that i don't know how can I represent in a plot the hour.
My goal is to represent a number as a datetime but I don't Know if it's possible to convert it as in the next example:
131212 --> HH:MM:SS --> 13:12:12
And then put this number in that format in an x-axis in a plot.
Thanks!!!

 Accepted Answer

Mischa Kim
Mischa Kim on 31 Jan 2014
Edited: Mischa Kim on 31 Jan 2014
This should do:
T0 = '131212';
T0Vec = [2000,0,0,str2double(T0(1:2)),str2double(T0(3:4)),str2double(T0(5:6))];
T0Str = datestr(T0Vec,'HH:MM:SS');
Once you have the string you can put it on the xlabel for your plot or add it as a text element.

14 Comments

Thanks for your response Mischa! Your code its works but If I prove my code in your code it doesn't works!
I believe that I don't Know how to pass the time! If you can help me I would be gratefully!
I have this code:
TIME_STAMP (100x1 cell) is like that:
TIME_STAMP 2014/01/16 13:12:12 2014/01/16 13:12:13 2014/01/16 13:12:16 2014/01/16 13:12:17 ...
I only have to work with the time and not with the date.
for i=1:length(TIME_STAMP) p=TIME_STAMP{i}(12); %first value of H f=TIME_STAMP{i}(13); %second value of H q=TIME_STAMP{i}(15); %first value of M r=TIME_STAMP{i}(16); %second value of M s=TIME_STAMP{i}(18); %first value of S t=TIME_STAMP{i}(19); %second value of S
%hours(i)=str2double(strcat(p,f));
%min(i)=str2double(strcat(q,r));
%seconds(i)=str2double(strcat(s,t));
time(i)=str2double(strcat(p,f,q,r,s,t));
%T0Vec = [2000,0,0,strcat(hours(i),min(i),seconds)];
T0Vec(i) = [2000,0,0,str2double(strcat(p,f)),str2double(strcat(q,r)),str2double(strcat(s,t))];
T0Str(i) = datestr(T0Vec(i),'HH:MM:SS');
end
- time (1x100 double) is like that:
131212 131213 131216 131217 ...
The result of my code:
Error: In an assignment A(I) = B, the number of elements in B and I must be the same.
I don't Know how to pass the time! If you can help me I would be gratefully! Once I have the string with the time I want to put into the x label. Thanks!!!
It's hard to see what's going on in your code...
  • There is an assignment error: A(I) = B, where does it occur?
  • If you already have time as a matrix of doubles, as you point out, simply use
T0 = num2str(time(i));
T0Vec = [2000,0,0,str2double(T0(1:2)),str2double(T0(3:4)),str2double(T0(5:6))];
T0Str = datestr(T0Vec,'HH:MM:SS');
Diego, read this http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup and then copy ALL the red text that you see, so we can give you an informed answer.
This is my code! the problem is that T0STr only shows the last position of the string and not appears all the position of the time!
I want to represent in x label the first time at 13:12:12 and the increasing of the time with the aim to show the variation of RSSI during the time.
Something like this (see attachment)?
It does work!!!! Thanks for your help! I'm very grateful! Thanks again Mischa!!!
Diego
Diego on 3 Feb 2014
Edited: Diego on 3 Feb 2014
Perfect! No problem. Thanks for your time!
Mika! I just saw that in your code when I represent the graphic in x label only shows somes values of TIME_STAMP. Theoretically in your code you put all the values of TIME_STAMP but only show until 13.12.26
The first value is 13.12.12, the last value 13.14.18, but only represent since 13.12.12 until 13.12.26.
Do you know what is the problem?
I attach you the code and the csv! Thanks Mika!
I suspect it has to do with the tick spacing. Try
set(gca,'XTickLabel',T0Str,'XTick',1:length(T0Str));
With this setting you force MATLAB to put one label per tick.
Would you mind formally accepting the answer? Thanks.
Ok it's perfect your answer but I have many values of TIME_STAMP and it is mixed when I represent it and it's impossible to understand...!
Another more thing, I want show up each value of RSSI in the plot. I know that manually when you represent the graphic you can put these values but one by one, automatically it's possible to do this? Do you know?
Thank you very much for your time Mika!
Yes, that's the problem. There're just too many labels, if you print them all.
About your second question: I am not sure I understand. Do you want to print a label right next to each RSSI data point? If so, check out this answer. If no, please further elaborate.
By the way, my name is Mischa :)
Yes I wanna do this! I'll try to do it!
I apologize for the confusion, I want to say Mischa! I was writing so quickly...!
Hello Diego. Good to see you again. Would you mind opening another question? It is not really on the topic of your original question, which would make it challening to find for other users. Plus this thread is getting kind of long.

Sign in to comment.

More Answers (0)

Asked:

on 31 Jan 2014

Commented:

on 13 Feb 2014

Community Treasure Hunt

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

Start Hunting!