Interpolation Error using interp1

I'm using a script to take these x and y values from a .txt file. At this point in the script the variables are assigned the fid is closed... When I code further, I'm trying to interpolate a user entered value which is called 'userx'.
Below, though, I am just trying to get it to interpolate at 100. I don't understand where my syntax error is.
>> x
x =
0
20
22
30
33
52
52
67
84
101
103
111
116
132
141
159
162
168
171
173
>> y
y =
8477
11886
11384
6087
7871
3003
1941
9213
10981
2179
2219
507
1593
8880
11650
4392
6118
4437
2094
1961
>> interp1(x,y,100) Error using interp1 (line 130) Values must be of type double or single.
>>

 Risposta accettata

Check the data types of x and }y}. My guess is that they are either cells or strings.
If they are cells, you can convert them to double arrays with:
x = x{:};
y = y{:};

4 Commenti

Jorge Bastillo
Jorge Bastillo il 28 Apr 2015
Modificato: Jorge Bastillo il 28 Apr 2015
I have a two columned .txt file, my script asks the user for the file name, checks available files, and locates it. The user input variable is fileput. I then assign the left column as x and the right column as y.
fid=fopen(fileput,'r');
data=textscan(fid,'%d %d','headerlines',1);
x=data{1};
y=data{2};
fclose(fid);
Now here in the command window I tried what you suggested... I think what you're saying is close to my problem.
>> x=x{:} Cell contents reference from a non-cell array object.
>> y=y{:}
Cell contents reference from a non-cell array object.
>> interp1(x,y,100)
Error using interp1 (line 130)
Values must be of type double or single.
>>
Not working though.
The output of textscan is a cell array, so your code should be giving you a numeric array. My best guess is that there are more than one header lines, or other problems with the file.
I would have to have your file to experiment with to understand the problem.
Jorge Bastillo’s ‘Answer’ moved here...
Also tried using cell2mat... Didn't work either.
>> a=cell2mat(x)
Cell contents reference from a non-cell array object.
Error in cell2mat (line 42)
cellclass = class(c{1});
That would mean to me that your conversion from cell to numeric with:
x=data{1};
y=data{2};
was successful (and correct), and that the problems are with non-numeric entries in ‘x’ and ‘y’.

Accedi per commentare.

Più risposte (1)

Try
x = double(x);
y = double(y);
to make sure they're doubles like interp1() said it wanted.

Categorie

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by