# How to shift my Data in X Direction?

조회 수: 32(최근 30일)
Khalil Gabsi 2019년 3월 10일
답변: Munish Raj 2019년 3월 19일
Dear community,
i have a question concerning interpolation. I have some curves looking like this: What i want to do now, ist to "shift" my Data, so that every maximum of the curve has the same X-Coordinate. I think that i should use the interp1 function? But i can´t solve it. What i´ve done so far:
[M1,IX1]=max(fid1_X_average);
[M2,IX2]=max(fid2_X_average);
[M3,IX3]=max(fid3_X_average);
[M4,IX4]=max(fid4_X_average); %to get the coordinate of the maximum value
[sizeX, Y]=size(fid1_X_average);
fid2_X_average=interp1(fid1_time-IX2 ,fid2_X_average, (0:1:sizeX));
But then i get something like that GREEN LINE (i don´t get the whole range of data i had before): So my maximum value is now at point zero, thats ok, but why has the data before zero been cut?
##### 댓글 수: 1표시숨기기 없음
Khalil Gabsi 2019년 3월 10일
to plot my data with an according x-value vector, is no solution for me (because of further use).
But theoretically i can use this functions and my data is plotted like i want:
plot(fid1_time-IX1, fid1_X_average)
plot(fid1_time-IX2, fid2_X_average)
plot(fid1_time-IX3, fid3_X_average)
plot(fid1_time-IX4, fid4_X_average) 댓글을 달려면 로그인하십시오.

### 답변(1개)

Munish Raj 2019년 3월 19일
Hello Khalil,
It makes sense that you can not efficienlty use interp1 to shift the maximum's as all of these waveforms have a different time axis.
If you really want to manually shift the data, you'll have to write an algorithm which shifts the waveforms by that much.
(However, you will lose data by this approach)
A sample code to shift teh waveform would be
Assuming you want to shift all the maximum's to 50,
[max,idx]=max(x);
s = zeros(size(x));
shift=max-50;
if shift >0
s(shift+1:end) = x(1:end-shift);
elseif shift <0
s(1:end+shift) = x(-shift+1:end);
end

댓글을 달려면 로그인하십시오.

### 범주

Find more on 2-D and 3-D Plots in Help Center and File Exchange

R2018b

### Community Treasure Hunt

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

Start Hunting!