Filter löschen
Filter löschen

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

3 Ansichten (letzte 30 Tage)
OcDrive
OcDrive am 6 Jan. 2024
Kommentiert: OcDrive am 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 Kommentare
Dyuman Joshi
Dyuman Joshi am 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 am 6 Jan. 2024
Thanks for the advice Dyuman, that's certainly an easier way of doing that

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Hassaan
Hassaan am 6 Jan. 2024
Bearbeitet: Hassaan am 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

Weitere Antworten (0)

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by