Table to timetable but the datetime doesn't like my data
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
OcDrive
el 6 de En. de 2024
Comentada: OcDrive
el 6 de En. de 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 comentarios
Dyuman Joshi
el 6 de En. de 2024
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)
Respuesta aceptada
Hassaan
el 6 de En. de 2024
Editada: Hassaan
el 6 de En. de 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
Más respuestas (0)
Ver también
Categorías
Más información sobre Dates and Time en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!