I have a question about matlab function 'interp2'

1 view (last 30 days)
Hi
I'm beginner matlab uer, so I need your help.
I made a program for my study.
Follow this is part of my program.
-------------------------------------------------------------------------------------------------------------------------------
function f=fct_trans(freq0,amp0)
toto1=load('TR_FCT.mat');
Phase=toto1.PHASE2;
Gain=toto1.GAIN2;
freq2=toto1.FREQ2;
freq2=freq2*2*pi;
amp2=0.1;
gain_ponct=interp2(freq2,amp2,Gain,[freq0],[amp0],'spline');
phase_ponct=interp2(freq2,amp2,Phase,[freq0],[amp0],'spline');
f=[gain_ponct phase_ponct];
-------------------------------------------------------------------------------------------------------------------------
An error occured like this
"??? Error using ==> interp2 at 147
X and Y must both be vectors or both be matrices."
What could i do??????
Please help me!!

Answers (1)

Walter Roberson
Walter Roberson on 31 Jul 2015
You assigned amp2=0.1 and then used that as your only "Y" coordinate. Your third matrix, "Gain" in this case must be either the same size as your "Y" (amp2) or else the third matrix must be length(Y) by length(X) -- so your Gain would have to be length(amp2) by length(freq2) which would be 1 by length(freq2). And it isn't.
You should only use interp2() when you have a 2D array of values, not when you have a vector of values. If you have only a vector of values then you would use interp1()
gain_ponct = interp1(freq2, Gain, freq0, 'spline'); %if Gain is a vector
If Gain is a 2D array whose entries are 0.1 apart in the first dimension, then:
amp2 = 0.1 * (1:size(Gain,1));
gain_ponct = interp2(freq2,amp2,Gain,[freq0],[amp0],'spline');
  1 Comment
splendider
splendider on 3 Aug 2015
Thank you! I accepted your advices and I changed my code interp2 -> interp1 but there is one problem on my code.
----------------------------------------------------------------------------------------------------------------------------------------
function f = fct_trans2(freq0)
calfreq1=load('TR_FCT_F.mat');
Phase=calfreq1.PHASE2; %Gain=calfreq1.GAIN2; freq2=calfreq1.FREQ2; omega2=freq2*2*pi; amp2=calfreq1.AMP2;
Gainsim=calfreq1.GAIN2sim; freq2sim=calfreq1.FREQ2sim; omega2sim=freq2sim*2*pi; amp2sim=calfreq1.AMP2sim;
gain_ponct=interp1(omega2sim,Gainsim,[freq0],'spline'); phase_ponct=interp1(omega2,Phase,[freq0],'spline'); %phase_ponct=1.7/1000*omega2sim;
f=[gain_ponct phase_ponct];
---------------------------------------------------------------------------------------------------------------------------------------------
the error : "X must be a vector."
All variables are matrix like '2 x ... ' accpet freq2 and amplitude2.
How I can do that????

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!