Convert day of year to UTC format?

How would I go about converting this format to matlab UTC format?
ie:
2012-259T01:53:15.50194
2012-259T01:53:16.50194

 Accepted Answer

T = ["2012-259T01:53:15.50194";
"2012-259T01:53:16.50194"];
T = datetime(T,'InputFormat','uuuu-DDD''T''HH:mm:ss.SSSSS')
T = 2×1 datetime array
15-Sep-2012 01:53:15 15-Sep-2012 01:53:16

12 Comments

This worked, thank you!
A follow up question which I apologize for if it's stupid but when I tried to select a specific cell in an imported file that had the original 'day of year' format, it returned NaN instead of '2012-259T01:53:15.50194'?
A(1,96)
ans =
NaN
If the data you have is stored in a cell array, you need to use curly brackets {} to access the data inside the element(s).
Try using A{1,96}
We'd need to know more about the variable, and how you imported the data from the file. You should still see the time value if you used () instead of {}. Instead, it appears your data hasn't been imported correctly.
The most likely reason is that you have tried to import the data as numeric - hence the NaN - when you should be importing it as either a char array or string, as it contains a mix of numbers, letters, and symbols.
Tried that and received this error.
Using () worked perfectly fine for other elements
A{1,96}
Brace indexing is not supported for variables of this
type.
All I'm doing is opening the file in matlab, selecting the specific columns I need, including the times column, and selecting the import green checkmark.
Cris LaPierre
Cris LaPierre on 14 Jul 2023
Edited: Cris LaPierre on 14 Jul 2023
I see all your values are <undefined>, which is why you are getting NaNs. This workflow is not loading the contents of your file correctly. Please attach your raw data file to your post using the paperclip icon. If necessary, zip your file first.
Can't do that sorry, I'll sort out on my own. In the meantime, would this block work if I'm trying to convert a column of 'day of year' format to standard UTC and plot the whole column vs another variable?
X being the column number of the other variable
for i = 1:1:9;
A = readmatrix("LRO_ES_0" + i. + "csv");
utc = datetime(A(:,96),'InputFormat','uuuu-DDD''T''HH:mm:ss.SSSSS');
plot(utc,A(:,X));
hold on;
end;
I don't think so. readmatrix is for reading in numeric data, and your data is a mix of numbers, letters, and symbols. Perhaps this is why you are getting <undefined>?
Try using readtable instead. See the Access Data in Tables page to use the data you read in.
As for whether it will work or not, the best way to find out is to run the code on your files and see if the result is what you expect.
Also, make sure you have a hold off in your code somewhere (after the 'end' perhaps).
Thank you so much! Using readtable as well as {} instead of () got everything working right.
Just an additional clarifying question since I'm new to matlab and still learning all the intricacies. If I'm making 3 separate subplots, or even if it wasn't, does adding a hold off actually do anything if I'm not plotting anything else after the loop?
It is a best practice to always pair a hold on with a hold off. You can usually get things to look the way you want without it. The impact is usually later when you rerun your script, or if you create new plots by plotting in the same figure window. It's best to get in the habit while you are learning.
Fair enough, thanks again

Sign in to comment.

More Answers (1)

Rahul
Rahul on 14 Jul 2023
Edited: Rahul on 14 Jul 2023
Hey Brandon,
The date time format provided by you, seemed to in the ISO 8601 format. Breaking down the format:
  • '2012-259': date as year and day of the year. In this case, it indicates the 259th day of the year 2012.
  • 'T': This is a separator indicating the start of the time component.
  • '01:53:15': time in hours, minutes, and seconds. In this case, it indicates 1 hour, 53 minutes, and 15 seconds.
  • '.50194': fractional part of the seconds. In this case, it indicates 50194 milliseconds.
You can use the following snippet to convert it to a standard UTC Timezone format:
inputTime = ['2012-259T01:53:15.50194'; '2012-259T01:53:16.50194'];
dt = datetime(inputTime, 'InputFormat', 'uuuu-DDD''T''HH:mm:ss.SSSSS', 'TimeZone', 'UTC')
dt = 2×1 datetime array
15-Sep-2012 01:53:15 15-Sep-2012 01:53:16
You can refer to the datetime function documentation to further customize the code as per your needs.
Hope that Helped!

Categories

Asked:

on 14 Jul 2023

Commented:

on 17 Jul 2023

Community Treasure Hunt

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

Start Hunting!