Effacer les filtres
Effacer les filtres

Table to timetable but the datetime doesn't like my data

3 vues (au cours des 30 derniers jours)
OcDrive
OcDrive le 6 Jan 2024
Commenté : OcDrive le 6 Jan 2024
Hello
I'm trying to convert my data into a timetable. It seems simple and straightforward but sill it doesn't work. This is the code:
NAO = readtable('NAOdaily.txt')
% Extract columns for datetime creation
year = NAO(:, 1);
month = NAO(:, 2);
day = NAO(:, 3);
% Create a datetime array
dateArray = datetime(year, month, day);
% Extract the remaining columns
values = NAO(:, 4);
% Create timetable
NAOTT = timetable(dateArray, values, 'VariableNames', {'Date', 'Value'});
Can you let me know at what point have I missed something? I'm attaching my txt file too.
Error using datetime
Invalid parameter name. Parameter name must be a nonempty string or character vector.
Error in LoadNAODaily (line 9)
dateArray = datetime(year, month, day);
Adding these didn't make a difference
dateArray = datetime('year', 'month', 'day');
Thanks for any assistance
  2 commentaires
Dyuman Joshi
Dyuman Joshi le 6 Jan 2024
If you are using R2019a or a newer version, use readmatrix to directly read the data as a matrix.
NAO = readmatrix('NAOdaily.txt');
% Extract columns for datetime creation
year = NAO(:, 1);
month = NAO(:, 2);
day = NAO(:, 3);
% Create a datetime array
dateArray = datetime(year, month, day);
% Extract the remaining columns
values = NAO(:, 4);
% Create timetable
NAOTT = timetable(dateArray, values)
NAOTT = 27733×1 timetable
dateArray values ___________ ______ 01-Jan-1948 -87.04 02-Jan-1948 -72.39 03-Jan-1948 -60.47 04-Jan-1948 -62 05-Jan-1948 -36.48 06-Jan-1948 -25.51 07-Jan-1948 -30.01 08-Jan-1948 -42.51 09-Jan-1948 -66.63 10-Jan-1948 -84.36 11-Jan-1948 -55.47 12-Jan-1948 11.01 13-Jan-1948 34.05 14-Jan-1948 34.89 15-Jan-1948 50.19 16-Jan-1948 89.74
OcDrive
OcDrive le 6 Jan 2024
Thanks for the advice Dyuman, that's certainly an easier way of doing that

Connectez-vous pour commenter.

Réponse acceptée

Hassaan
Hassaan le 6 Jan 2024
Modifié(e) : Hassaan le 6 Jan 2024
The correct way to create a datetime object in MATLAB from separate year, month, and day columns is to ensure that these columns are extracted as numeric arrays, not as table variables. When you read the data using readtable, you should use the specific column names or indices. Since your text file doesn't have headers, you would use indices like NAO{:, 1} to get the data.
NAO = readtable('NAOdaily.txt', 'Format', '%d %d %d %f'); % Specify the format of the data
% Extract columns for datetime creation
year = NAO{:, 1};
month = NAO{:, 2};
day = NAO{:, 3};
% Create a datetime array
dateArray = datetime(year, month, day);
% Extract the values column and ensure it's a vector
values = NAO{:, 4};
% Create timetable
NAOTT = timetable(dateArray, values);
head(NAOTT, 5)
Output
dateArray values
___________ ______
01-Jan-1948 -87.04
02-Jan-1948 -72.39
03-Jan-1948 -60.47
04-Jan-1948 -62
05-Jan-1948 -36.48
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering

Plus de réponses (0)

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by