Interpolate multiple data for a plot

1 次查看(过去 30 天)
I am having trouble interpolating data. I don't quite know what variables to insert into interp1. I need to plot sea against temperature after interpolating as the time for sea and time for weather is different. Any tips?
for k=1:7
figure
w{k} = interp1(timeweather{k},avgtemp{k},sealevel{k},'linear');
plot(timeweather{k},avgtemp{k},'-',5,w{k},'*')
plot(timeweather{k},avgtemp{k},'r')
hold on
plot(timesea{k},sealevel{k},'c')
end
  3 个评论
Madlab
Madlab 2018-10-17
The time format is in decimal years for both timeweather and timesea.
Yes, each corresponds to a different location. For location X, the sealevel is sealevel{1} for timesea{1} while the temperature is avgtemp{1} for timeweather{1}. I need to interpolate to see how the temperature and sealevel are related.
Likewise, for location Y, the sealevel is sealevel{2} for timesea{2} while the temperature is avgtemp{2} for timeweather{2}. I need to interpolate to see how the temperature and sealevel are related.
And this continues for the different locations...
Madlab
Madlab 2018-10-17
Sorry, but I do not understand what you mean by "use a timetable and synchronize." Isn't interp1 supposed to help me synchronise the data? I am very confused.

请先登录,再进行评论。

采纳的回答

jonas
jonas 2018-10-17
Normally I'm not a big fan of fraction years, but here we go:
% Common time vector
tc = 1850:.1:2020;
% interpolate
out.sealevel = cellfun(@(x,y)interp1(x,y,tc),timesea,sealevel,'uniformoutput',false)
out.temp = cellfun(@(x,y)interp1(x,y,tc),timeweather,avgtemp,'uniformoutput',false)
all time-series stored in the struct out can be plotted against the common time vector tc.
  2 个评论
Madlab
Madlab 2018-10-18
编辑:Madlab 2018-10-18
Hey Jonas, thank you for the input. I would like to ask though, what is the use of x and y? I know that cellfun applies a function to each cell of a cell array, which in this case is timesea,sealevel.
So this interpolates all the data for each respective cell with the help of cellfun. May I also ask what is the use of false behind uniformoutput?
jonas
jonas 2018-10-18
This is the function
@(x,y)interp1(x,y,tc)
x and y are the variables. Cellfun applies this function to each cell in the inputs that come as the 2nd and 3rd arguments in this case.
...'uniformoutput',false
just means that the output cells does not have to be of the same size. It may not be necessary in this case.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by