error using ts/totable (line 63) duplicate table row name: '29-oct-2023 02:00:00' in matlab

In MATLAB, the error "duplicate table row name" usually occurs when trying to convert a timetable to a table using table or other functions such as table2array, and it cannot create unique row names due to duplicates in the timetable. To avoid this error, you can convert the timetable to a table and handle the duplicates manually or modify your data to ensure unique row names.

Here's an example of how you can handle this error by renaming rows with duplicate names in a timetable before converting it to a table:

main.m
% Sample timetable with duplicate row names
tt = timetable('RowTimes', datetime(2023,10,29,2,0,0) + hours(0:2)', ...
    'Temperature', [25; 26; 27], 'Humidity', [60; 65; 70]);

% Check for duplicate row times
duplicateRows = tt.Properties.RowTimes.duplicated;
if any(duplicateRows)
    % Create unique row times for duplicates
    tt.Properties.RowTimes = tt.Properties.RowTimes + milliseconds(round(rand(sum(duplicateRows),1)*1000));
end

% Convert the timetable to a table
tbl = table(tt);

disp(tbl);
505 chars
16 lines

In this example, we check for duplicate row times in the timetable tt, and if duplicates are found, we add random milliseconds to make them unique. Then, we convert the modified timetable to a table tbl without encountering the "duplicate table row name" error.

By ensuring unique row names in the timetable before converting it to a table, you can avoid the error and work with the table data effectively.

related categories

gistlibby LogSnag