Date/Time Processing and Formatting Issues

7 views (last 30 days)
I'm very new to MATLAB and have been reading tutorials and posts on plotting data from an external file (csv) and am having trouble processing date/time data. The CSV file has the date/time field as yyyy:ddd:hh:mm:ss.sssssssss (inculded sample below).
I have tried several ways to plot the data but always run into an error when trying to convert the date/time string into date/time (I'm sure I'm missing a very basic/fundamental item), inculding different variations of
[data,txt,raw] = xlsread('test.csv');
x = txt{2:end,1};
y = raw(:,1) ;
dt = datetime(x,'InputFormat', '');
Appreciate any help!
  1 Comment
F_M on 5 Oct 2022
Moved: Star Strider on 5 Oct 2022
Thanks again. The table is attached.

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 4 Oct 2022
The 'yyyy' needs to be 'uuuu' (ISO year) to avoid problems with the conversion —
C = {'2022:246:10:00:02.593994140',4.44E+01
T = cell2table(C)
T = 3×2 table
C1 C2 _______________________________ ____ {'2022:246:10:00:02.593994140'} 44.4 {'2022:246:10:00:03.592987060'} -76 {'2022:246:10:00:04.092987060'} 225
T.C1 = datetime(C(:,1),'InputFormat','uuuu:DDD:HH:mm:ss.SSSSSSSS')
T = 3×2 table
C1 C2 ____________________ ____ 03-Sep-2022 10:00:02 44.4 03-Sep-2022 10:00:03 -76 03-Sep-2022 10:00:04 225
Star Strider
Star Strider on 5 Oct 2022
My pleasure!
The xlsread function has been superceded by others and is now ‘Not recommended’ (and may be deprecated in future releases), so I have stopped using it in MATLAB Answers for compatibility reasons. (Also, it doesn’t work with the online Run feature.) I now use readtable or readmatrix instead.
I was able to make my datetime code work after creating the appropriate vectors using compose. Given the format of your file, I doubt if it would be possible to use detectImportOptions to get the same result.
Anyway, my datetime code worked with it, so I learned something by figuring out how to create the appropriate character arrays to use with datetime in files with unusual formats.

Sign in to comment.

More Answers (1)

millercommamatt on 4 Oct 2022
dt = datetime(x,'InputFormat', 'yyyy:DDD:HH:MM:ss.SSSSSSSSS');
F_M on 4 Oct 2022
I tried it with the same errors:
With x=txt{2:end,1};
Unable to perform assignment with 0 elements on the right-hand side.
Error in test_plot_a (line 2)
x = txt{2:end,1};
With x=txt(:,1);
Warning: The format 'yyyy:DDD:HH:mm:ss.SSSSSSSSS' contains fields for day of year (D) and Gregorian year (y). This will cause
unexpected results when converting from text. Use ISO year (u) in place of Gregorian year (y). See the datetime.Format
property for a complete description of the identifiers used in datetime formats.
> In datetime (line 636)
In test_plot_a (line 4)
Error using datetime (line 651)
Unable to convert 'pixel0' to datetime using the format 'yyyy:DDD:HH:mm:ss.SSSSSSSSS'.
Error in test_plot_a (line 4)
dt = datetime(x,'InputFormat', 'yyyy:DDD:HH:mm:ss.SSSSSSSSS');

Sign in to comment.





Community Treasure Hunt

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

Start Hunting!

Translated by