Error using scatteredInterpolant on missing data
14 次查看(过去 30 天)
显示 更早的评论
Hello everyone,
I would like to use function scatteredInterpolant or interp1 to compare results with fillmissing function and inpaint_nans on the leading or trailing missing values (NaN)/ peak at the edge as attached file.
However, it has this error when I tried to input the data. I tried to read some instruction on MATLAB, but my data, it does not have any functions of x and y for z. Could anyone please help me figure out the reason to input the data properly?.
Thank you very much!
T = readtable ('data.csv')
data = table2array(T) ;
x = data(1,:) ; x(1) = [] ;
y = data(:,1) ; y(1) = [] ;
z = data(2:end,2:end) ;
F = scatteredInterpolant(x,y,z)
Error using scatteredInterpolant
The input points must be specified in column-vector format.
0 个评论
采纳的回答
Cris LaPierre
2022-11-22
When using the syntax F = scatteredInterpolant(x,y,v), your inputs must all be vectors. That means v=z(:), and that x and y are the same length as v. Try something like this.
T = readtable ('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1203033/data.csv');
data = table2array(T) ;
x = data(1,:) ; x(1) = [] ;
y = data(:,1) ; y(1) = [] ;
z = data(2:end,2:end) ;
[X,Y] = meshgrid(x,y);
F = scatteredInterpolant(X(:),Y(:),z(:))
% visualize the result
[xq,yq] = meshgrid(linspace(400,450),linspace(150,200));
vq = F(xq,yq);
plot3(X(:),Y(:),z(:),'b.',xq,yq,vq,'r.')
更多回答(1 个)
Kai
2022-11-22
Hi Ngoc, so for the scatteredInterpolant function, it's goal is to get the interpolant based on given x y z coordinates and the corresponding values on these coordinates. Based on your csv file, I am assuming you are trying to interpolate 2D data. In this case will be F = scatteredInterpolant (x,y,v), which the function itself is trying to get the F in v = F(x,y). After F is calculated, you can bring in the sampled point coordinate (x_s,y_s) in to F(x_s,y_s) to get the interpolate values.
As for the column-vector error, the scatteredInterpolant’s input data must be in sizes of m x 1, you can use transpose or reshape to change it into column-vector format to solve the error.
Also based on your code, you only extracted the values of x and y, but didn't match them into coordinate (x,y), and that is why the size of x and y arrays are different. I will recommend checking this example in the link and specifically the size differences of the sample array used in it: 2D interpolant sample
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!