How do I combine three cells to output a date time?

My data is a .txt file with the first three fields beining the date time in the following format:
year doy timez
2017 100 04:03:00 Z
I used
D=datevec(datenum(year,1,doy));
D=datetime(D);
to get the year, month, day and 00:00:00
but I can't seem to get the timez to parse out. timez was imported into a table as a cell whereas year and doy were imported as double.

3 Comments

Can you provide a sample text file that you are using as well as the desired output? Are you trying to print the date and time to the Command Window or store it as a variable?
You used "D=datevec(datenum(year,1,doy))", but how did you obtain "year" and "doy"?
Thanks for the response. I was using the 3 delimited values (listed above) to create a datetime variable for plotting and calculations. The delimited text file comes with 23 fields with no variable names, of which I need 8. Baby steps for me at this point. Thanks again.

Sign in to comment.

 Accepted Answer

You would be better off fixing your import so that the whole date is imported as just one column of your table. For that, we need to know the original format of the file.
After the fact, probably the easiest to fix the mess is to recreate a string from your table variables and parse it with datetime. I'm using compose for that, which requires at least R2016b:
dates = compose('%4d %3d %s', yourtable.year, yourtable.day, string(yourtable.timez));
dates = datetime(dates, 'InputFormat', 'yyyy DDD HH:mm:ss Z', 'TimeZone', 'UTC')

1 Comment

Thank you Sir! I was unfamiliar with 'compose'. I get this data regularly in a .txt file with 23 delimited values and no headers. 'doy' was just my variable describing the content. I was trying some different approaches to importing the data (23 columns of which I use 8) that have time and position data. Time was my big stumbling block. Thank you again, the compose format will lead me to where I need to go from here. (I hope)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!