# Error using matlab.int​ernal.math​.interp1, Sample points must be unique.

256 views (last 30 days)
Furkan Sencer Kaçar on 24 Oct 2023
Commented: Steven Lord on 25 Oct 2023
I am getting this error when I run the attached matlab code. I couldn't figure out how to solve it and I would be very happy if you can help me.
dpb on 24 Oct 2023
Edited: dpb on 25 Oct 2023
Good sidebar comment @Steven Lord -- I wasn't aware that 'MarkerIndices' had been added to line properties....or maybe it's always been there and I just didn't recall it; I know I've combed the list in its entirety before.
Steven Lord on 25 Oct 2023
It was introduced in release R2016b.

Rik on 24 Oct 2023
Edited: Rik on 24 Oct 2023
interp1 is not intended to fit equations. There are other tools to do that. If you insist on using this function, you will either have to calculate some consensus value for each unique x (e.g. the mean, the median, the maximum, the minimum, ...), or you should adjust the x values slightly so each value becomes unique.
Note that the order of your x-values suddenly matters for the second option, because each element will have a different offset based on the position.
x = [1 1 1 2 3];
y = [0 1 2 3 4];
xq = 1:3;
x2 = 1×3
1 2 3
y2 = 1×3
1 3 4
x3 = 1×5
1.0000 1.0000 1.0000 2.0000 3.0000
y3 = 1×5
0 1 2 3 4
x4 = 1×3
1 2 3
y4 = 1×3
1.0625 2.6250 4.1875
plot(x,y,'b*','DisplayName','raw data'),axis([0.5 3.5 -0.5 4.5])
hold on
hold off
legend('Location','SouthEast')
% Add a different amount to each x-value to make them unique.
% The sum of the shift should be equal to 0 to avoid an overall drift.
shift = 1:numel(x);
shift = shift - mean(shift);
end
% For each unique x, only keep the median y-value
[new_x,ignore,ind] = unique(x);
new_y = accumarray(ind,y,[numel(new_x) 1],@median);
if isrow(x),new_y = new_y.';end % Keep directions consistent
end
p=polyfit(x,y,1);
x=unique(x);
y=polyval(p,x);
end

dpb on 24 Oct 2023
Edited: dpb on 24 Oct 2023
As the follow on comment above shows, you can safely ignore the duplicate points and retain the original function -- use
[~,iu]=unique(data(:,1));
data=data(iu,:);
in place of the full data array to reduce it to the set of unique times and associated values; then pass the resulting time, response vectors to the function instead and all should work just fine...although there might be a question regarding there being two transients in the overall trace? Or is this an input concentration to be modelled its dispersion, maybe, in which case it could be intended and correct--we don't know anything about the problem trying to solve, just that there were duplicated times in this input trace that aren't allowed by the construction of the function.