Plot distance with time

Hi team,
I have an excel file with data in the following format.
serial time ID x
28203 1 341.557
28203 2 374.237
28203.2 1 339.891
28203.2 2 372.506
28203.3 1 338.224
28203.3 2 370.709
28203.5 1 336.558
28203.5 . 2 368.849
serial time is from 12am.I have many vehicle ID s and im trying to plot the graph of x against time for various time's.
Tab=readtable('Trial1.xlsx');
T=Tab{:,2:4};
x=max(T(:,2));
for n=1:x
A=T(T(:,2)==n,:);
plot(A(:,1),A(:,3),'DisplayName',num2str(n));
hold on;
end
hold off
grid on
end
My questions are,
1.is there a better way to do this?
2.how to convert serial time to actual time for the x axis ('HH:MM:SS' format)?
Thanks

4 Comments

Is it possible that your actual times are, for example, 16-Mar-1977 04:48:00 ?
Which MATLAB release are you using?
I am using 2017b. Yes it is correct, i need times such as 7:20:25 am
Walter Roberson is basically asking if the serial time is already in MATLAB's serial date format which is (date.time), so evaluating your first datapoint in MATLAB give the date of March 1977. Whereas your data is (I believe...) simply time data without the date. What you can do is use the function datestr() to convert your data to time, but since you have time data you will need to add 6 digits for the date. Something like this 737068 is today's date(you can try datenum(now)). so if you try
datestr(737068.28203)
for your first data sample it should be correct(will give you 06:46)
No, I suspect that the number is excel serial date format. Converting from that format gives dates in 1977.

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 8 Jan 2018

0 votes

Take the second column of your data and use it as input to datetime with the option 'convertfrom', 'Excel'. You can use the result as the x axis directly in R2016b or later. You can use the Format option of the datetime call to control how the dates appear on the axes.

4 Comments

Thank you, yes it worked. Could you please explain how I can use the result as the x axis directly?
AS the converted array os of char, and the other values are double, wouldnt that matter?
Example:
Tab = readtable('Trial1.xlsx');
excel_timestamp = Tab{:,2};
timestamp = datetime(excel_timestamp, 'ConvertFrom', 'excel');
data_to_plot = Tab{:,4};
plot(timestamp, data_to_plot);
walter Roberson do you have an idea how i plot a grid for path planning ,please help me
That is an unrelated matter that should be in its own Question.
Look at the grid command.

Sign in to comment.

I'm gonna make a different guess: those numbers are seconds since midnight. So:
>> t = readtable('Trial1.xls');
Warning: Variable names were modified to make them valid MATLAB identifiers. The original names are saved in the
VariableDescriptions property.
>> head(t)
ans =
8×12 table
identifier TimeStamp_sinceMidnight_ idVeh xCurrentPos yCurrentPos zCurrentPos CurrentSpeed PreviousSpeed idSection_idJunction xCurrentPosBack yCurrentPosBack zCurrentPosBack
__________ ________________________ _____ ___________ ___________ ___________ ____________ _____________ ____________________ _______________ _______________ _______________
1 28200 1 373.22 22.983 0 40 40 449 385.22 22.822 0
2 28200 1 371.56 23.005 0 40 40 449 383.55 22.844 0
3 28200 1 369.89 23.027 0 40 40 449 381.89 22.866 0
4 28201 1 368.22 23.05 0 40 40 449 380.22 22.889 0
5 28201 1 366.56 23.072 0 40 40 449 378.55 22.911 0
6 28201 1 364.89 23.095 0 40 40 449 376.89 22.933 0
7 28201 1 363.22 23.117 0 40 40 449 375.22 22.956 0
8 28201 1 361.56 23.139 0 40 40 449 373.56 22.978 0
>> t.TimeStamp_sinceMidnight_ = duration(0,0,t.TimeStamp_sinceMidnight_);
>> head(t)
ans =
8×12 table
identifier TimeStamp_sinceMidnight_ idVeh xCurrentPos yCurrentPos zCurrentPos CurrentSpeed PreviousSpeed idSection_idJunction xCurrentPosBack yCurrentPosBack zCurrentPosBack
__________ ________________________ _____ ___________ ___________ ___________ ____________ _____________ ____________________ _______________ _______________ _______________
1 07:50:00 1 373.22 22.983 0 40 40 449 385.22 22.822 0
2 07:50:00 1 371.56 23.005 0 40 40 449 383.55 22.844 0
3 07:50:00 1 369.89 23.027 0 40 40 449 381.89 22.866 0
4 07:50:00 1 368.22 23.05 0 40 40 449 380.22 22.889 0
5 07:50:00 1 366.56 23.072 0 40 40 449 378.55 22.911 0
6 07:50:00 1 364.89 23.095 0 40 40 449 376.89 22.933 0
7 07:50:01 1 363.22 23.117 0 40 40 449 375.22 22.956 0
8 07:50:01 1 361.56 23.139 0 40 40 449 373.56 22.978 0
>> tt = table2timetable(t,'RowTimes','TimeStamp_sinceMidnight_');
>> head(tt)
ans =
8×11 timetable
TimeStamp_sinceMidnight_ identifier idVeh xCurrentPos yCurrentPos zCurrentPos CurrentSpeed PreviousSpeed idSection_idJunction xCurrentPosBack yCurrentPosBack zCurrentPosBack
________________________ __________ _____ ___________ ___________ ___________ ____________ _____________ ____________________ _______________ _______________ _______________
07:50:00 1 1 373.22 22.983 0 40 40 449 385.22 22.822 0
07:50:00 2 1 371.56 23.005 0 40 40 449 383.55 22.844 0
07:50:00 3 1 369.89 23.027 0 40 40 449 381.89 22.866 0
07:50:00 4 1 368.22 23.05 0 40 40 449 380.22 22.889 0
07:50:00 5 1 366.56 23.072 0 40 40 449 378.55 22.911 0
07:50:00 6 1 364.89 23.095 0 40 40 449 376.89 22.933 0
07:50:01 7 1 363.22 23.117 0 40 40 449 375.22 22.956 0
07:50:01 8 1 361.56 23.139 0 40 40 449 373.56 22.978 0
I'm guessing you then want to do something like
j = (tt.idVeh == i)
plot(tt.Time(i),tt.xCurrentPos(i))
in a loop for i = 1:max(tt.idVeh).

Products

Tags

Asked:

on 8 Jan 2018

Answered:

on 9 Jan 2018

Community Treasure Hunt

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

Start Hunting!