필터 지우기
필터 지우기

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

조회 수: 3 (최근 30일)
OcDrive
OcDrive 2024년 1월 6일
댓글: OcDrive 2024년 1월 6일
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
Dyuman Joshi
Dyuman Joshi 2024년 1월 6일
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 2024년 1월 6일
Thanks for the advice Dyuman, that's certainly an easier way of doing that

댓글을 달려면 로그인하십시오.

채택된 답변

Hassaan
Hassaan 2024년 1월 6일
편집: Hassaan 2024년 1월 6일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by