MATLAB: How to use pchip to interpolate between data points in cartesian coordinate format

interpolationpchip

I am trying to use pchip to interpolate between a series of data points. My data are spatial (lat/long) but have been converted into meters in a projected cartesian coordinate system (UTM 31N- i.e. x= 431804m, y=4571664m).
The code that I am using is below, but for some reason I get error messages when trying to create objects 't' and 'p'. long_m is a 1xcolumn matrix of longitude in meters, lat_m is a 1xcolumn matrix of latitude in meters, 0.01667= 1/60th in decimal, specifying that I want to end up 60 times more data points than I have now via the interpolation:
x=long_m;
y=lat_m;
t=x(1,1):0.016667:x(211,1);
p=pchip(x,y,t);
Output message:
t =
Empty matrix: 1-by-0
p =
Empty matrix: 1-by-0
I thought that the function would return a string of numbers that relate to my interpolated points, but instead I end up with an empty 't' matrix and an empty 'p' matrix. What am I doing wrong?
Some example data is below:
x= [518666 521872 519984 519591 518800];
y= [4694989 4667173 4644884 4645622 4647104];
I am new to matlab (although I have knowledge of programming language from R), so any advice would be greatly appreciated.

Best Answer

The all-increasing or all-decreasing issue you're encountering isn't a restriction of pchip, but rather of the : operator. If you try to define a vector with a positive step size but decreasing values, you'll get an empty vector. For example:
>> 5:1:2
ans =
Empty matrix: 1-by-0
I'm guessing that what you're really after in this case is a parametric interpolation, where you "fill in" points in both the x- and y-direction.
% Some fake data
theta = linspace(0, 2*pi, 10);
x = cos(theta);
y = sin(theta);
% Interpolate
told = 1:10;
tnew = linspace(1,10,50);
xnew = pchip(told, x, t);
ynew = pchip(told, y, t);
% Plot
plot(x,y,'-b.', xnew, ynew, '-ro');
You can get more sophisticated to evenly space your final points, but this gives you there general idea.