Error using griddedInterpolant Sample points must be unique!

23 views (last 30 days)
Hi, I am trying to generate following code
figure(),hold;
x_points = [-35:0.25:35]
files = dir('rad_cldn2*');
n= zeros(size(x_points,2),length(files));
for i=1:length(files)
eval(['load ' files(i).name]);
data = dlmread (files(i).name);
samplePoints = {(data (:,1)), 1:size(data,2)};
F = griddedInterpolant(samplePoints,data);
queryPoints = {x_points,1:size(data,2)};
dataq = F(queryPoints);
x = dataq(:,1);
y = dataq(:,2);
n (:,i) = y;
plot(x,y,'DisplayName', files(i).name);
end
m = mean (n,2);
plot(x,m,'Linewidth',2,'Displayname','average');
But it's showing me error: "Error using griddedInterpolant.Sample points must be unique."So I assumed there must be duplicate data and that's why it's showing that error! So I have tried to use unique() command and then it's showing error : "Error using griddedInterpolant! Sample points vector corresponding to grid dimension 1 must contain 508 elements." which may be means there is a mismatch between sample points vector and grid dimension! Do anyone have any clue how to solve this error? thank you!

Answers (1)

Alan
Alan on 29 May 2024
Hi Ridaka,
It has been 3 years, but if you are still looking for a solution here could be one. The function griddedInterpolant tries to learn a function from given data. Here is the documentation link: https://www.mathworks.com/help/releases/R2021a/matlab/ref/griddedinterpolant.html
You are trying to perform 1-D interpolation in the following line:
F = griddedInterpolant(samplePoints,data);
It is required that samplePoints contain unique elements, otherwise the interpolant will be confused as to what value to map the duplicate values to. For example, if you define:
F = griddedInterpolant([1 1], [2 3])
, there is no function that satisfies both f(1) = 2, as well as f(1) = 3.
It is also required that samplePoints contain the same number of elements and the same shape as data. Here is an example to demonstrate the same:
Suppose you define:
F = griddedInterpolant([1 2 3], [10 20])
, there is no way to correctly map each point of samplePoints to one in data, as there is an extra element in samplePoints.
Here is sample code to demonstrate the above mentioned issues:
Correct usage:
x = 0:0.1:1 * 2 * pi;
v = sin(x);
F = griddedInterpolant(x, v);
Wrong usage 1:
x2 = x;
x2(2) = x2(3);
% Now the function F(x2, v) is invalid since x2(2) = x2(3)
% but F(x2(2)) != F(x2(3)). Hence, the interpolation cannot
% happen
F = griddedInterpolant(x2, v);
This will give the following error:
Wrong usage 2:
v2 = v;
v2(end) = []; % Removing last element
disp("Size of x = [" + num2str(size(x)) + "]");
disp("size of v2 = [" + num2str(size(v2)) + "]");
F = griddedInterpolant(x, v2);
This will give the following error:
On a side note, it is not recommended to use dlmread as of R2019a as mentioned at the end of this page:
I hope this helped.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!