Table to timetable but the datetime doesn't like my data
4 ビュー (過去 30 日間)
古いコメントを表示
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
2024 年 1 月 6 日
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)
採用された回答
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 Center および File Exchange で Dates and Time についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!