Error in interp2 (interpolation) command.

I woyld like to intepolate my data. I have an ascii file with 3 columns (x,y,z). Each column has 700 lines (number). I am using the following commands, in order to interpolate (smooth) my data:
clc
clear
filename1= 'mydata.csv';
[d1,tex]= xlsread(filename1);
y=d1(:,3);
x=d1(:,4);
z=d1(:,5);
[xq, yq] = meshgrid(...
linspace(min(x),max(x)),...
linspace(min(y),max(y)));
zq = interp2(x,y,z,xq,yq,'cubic');
[c,h]= contourf(xq,yq,zq);
but command window shows me:
Error using griddedInterpolant
The grid vectors must be strictly monotonically increasing.
Error in interp2>makegriddedinterp (line 228)
F = griddedInterpolant(varargin{:});
Error in interp2 (line 128)
F = makegriddedinterp({X, Y}, V, method,extrap);
could you please help me?

댓글 수: 3

Torsten
Torsten 2022년 10월 22일
"If X and Y are matrices representing a full grid (in meshgrid format), then V must be the same size as X and Y."
x and y are vectors.
Ivan Mich
Ivan Mich 2022년 10월 22일
I am uploading a sample of data file
Torsten
Torsten 2022년 10월 22일
No, the two inputs being provided to the INTERP2 are not vectors, but are in fact the (matrix) outputs from MESHGRID.
The inputs x,y and z to interp2 the OP provides are vectors (which is wrong). The query values xq and yq are matrices obtained from "meshgrid".

댓글을 달려면 로그인하십시오.

답변 (2개)

Torsten
Torsten 2022년 10월 22일
편집: Torsten 2022년 10월 22일

1 개 추천

Read about the requirements for input arguments x, y and z for interp2:
Paragraph "Input Arguments".

댓글 수: 4

Ivan Mich
Ivan Mich 2022년 10월 22일
I have already read it, but I do not understand which command should I use about xq and yq in my script? Could you please help/explain me?
Torsten
Torsten 2022년 10월 22일
So your x and y are strictly monotonic vectors and your z is a matrix with length(y) rows and length(x) columns ? If I look at your code, this does not seem to be the case.
Your data are not gridded. Thus you will have to use "scatteredInterpolant":
F = scatteredInterpolant(x,y,z);
[xq,yq] = meshgrid(linspace(min(x),max(x)),linspace(min(y),max(y)));
zq = F(xq,yq)
Confirm that your x and y vectors are sorted.
issorted(x, 'strictascend')
issorted(y, 'strictascend')
One or both of those will return false. Once you know which one(s) are not strictly ascending, you need to determine why. Plotting that vector may help you identify where the flat, decreasing, or missing segments are.

댓글을 달려면 로그인하십시오.

Torsten
Torsten 2022년 10월 22일
d1 = readmatrix("https://de.mathworks.com/matlabcentral/answers/uploaded_files/1165673/mydata.csv");
y=d1(:,3);
x=d1(:,4);
z=d1(:,5);
F = scatteredInterpolant(x,y,z,'nearest');
Warning: Duplicate data points have been detected and removed - corresponding values have been averaged.
[xq,yq] = meshgrid(linspace(min(x),max(x)),linspace(min(y),max(y)));
zq = F(xq,yq);
contourf(xq,yq,zq)
colorbar

댓글 수: 3

Ivan Mich
Ivan Mich 2022년 10월 24일
ok thank you, but the point is that i would like to "smooth" these contours that you show me in the result image as it produced. So, I think that interpolation would be suitable, but the resultedf contours are "iritable" (not smoothed).
Is there a way to make it?
Stephen23
Stephen23 2022년 10월 24일
@Ivan Mich: you could try a thin-plate spline: https://www.mathworks.com/help/curvefit/tpaps.html
Torsten
Torsten 2022년 10월 24일
Your z-data are not continuous - they only take values 1 1.5 2 2.5 3 ... 9. So would continuous values as 3.1865 even make sense for your application ?
And if you look at the contour plot from your raw data above: the only thing you can learn is that the values for the south-east part are lower than those for the north-west part.
Everything else like further smoothing would not be appropriate in my opinion.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Contour Plots에 대해 자세히 알아보기

질문:

2022년 10월 22일

댓글:

2022년 10월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by