error check for unique values of dates data set and pick one over the other if duplicated in in matlab

You can use the unique() function in MATLAB to identify the unique values in a dataset. To select one of the duplicated values over the other, you can use the unique() function with the 'last' or 'first' argument, which returns the index of the last or first occurrence of a duplicated value, respectively. Here's an example:

Let's say you have a dataset dates as shown below, and you want to identify the unique values and select the first occurrence of each unique value:

main.m
dates = ["2021-05-01", "2021-05-02", "2021-05-01", "2021-05-03", "2021-05-01"];

% Identify unique values
[uniqueDates, index] = unique(dates);

% Select first occurrence of each unique value
uniqueDatesFirstOccurrence = dates(sort(index));
241 chars
8 lines

In this example, uniqueDates will be ["2021-05-01", "2021-05-02", "2021-05-03"], and uniqueDatesFirstOccurrence will be ["2021-05-01", "2021-05-02", "2021-05-03"].

If you want to select the last occurrence of each unique value instead, you can pass the argument 'last' to the unique() function:

main.m
% Select last occurrence of each unique value
uniqueDatesLastOccurrence = dates(sort(index, 'descend'));
105 chars
3 lines

In this case, uniqueDatesLastOccurrence will be ["2021-05-03", "2021-05-02", "2021-05-01"].

gistlibby LogSnag