My interpolation works for one set, but does not work for the other

7 views (last 30 days)
Hi,
I am using the following code for interpolation from data in files. The problem is that the code runs very nicely in the first set, X1, Y1, while it shows that the X and V values don't have the same length in the second set: X2, Y2. I can't figure out the problem. Both codes present the same type of results and have same column length. The files of both sets are attached herewith.
clear; clc;
y=load ('Y1.txt');
x=load ('X1.txt');
plot(x(:,2),-sum(y(:,2:11),2))
y1=-sum(y(:,2:11),2);
x1=x(:,2);
y3=unique(y1);
x3=unique(x1);
y2 = [1308.9, 2514.9, 4797.9]*1000;
x2 = interp1(y3, x3, y2, 'linear');
figure(1)
plot(x1, y1, '-g')
hold on
plot(x2, y2, 'bp')
hold off
grid
legend('Data', 'Interpolated Points', 'Location', 'NW')
  1 Comment
Ismail Qeshta
Ismail Qeshta on 12 Nov 2017
Edited: Ismail Qeshta on 12 Nov 2017
Error message is the following:
Error using interp1>reshapeAndSortXandV (line 423)
X and V must be of the same length.
Error in interp1 (line 92)
[X,V,orig_size_v] = reshapeAndSortXandV(varargin{1},varargin{2});
Error in Interpolate (line 15)
x2 = interp1(y3, x3, y2, 'linear');

Sign in to comment.

Accepted Answer

Jan
Jan on 12 Nov 2017
Edited: Jan on 12 Nov 2017
The message is clear:
X and V must be of the same length.
Set a breakpoint in the failing line and check the dimensions:
size(y3)
size(x3)
Or:
y = load('Y2.txt');
x = load('X2.txt');
y1 = -sum(y(:,2:11),2);
x1 = x(:,2);
y3 = unique(y1);
x3 = unique(x1);
size(y3)
size(x3)
ans =
5504 1
ans =
5501 1
Then an interpolation is not possible. A solution might be:
[x3, ix] = unique(x1);
y3 = y1(ix);
  2 Comments
Ismail Qeshta
Ismail Qeshta on 12 Nov 2017
Hi Jan,
Yes. You are right. I have just checked it. Do you have any idea on how to solve this issue? Thanks.
a= size(y3)
b= size(x3)
a =
1220 1
b =
1215 1

Sign in to comment.

More Answers (0)

Categories

Find more on Interpolation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!