Interpolation Error using interp1 for interval 30 minutes
11 次查看(过去 30 天)
显示 更早的评论
Hi everyone,
Could you help me check my below code? I'm trying with many ways, however, It always met the error
% %%reset
clear all;
close all;
clc;
%delete NaN, continuously duplicated value and keep the last one
f=fopen('CLF1997.txt');
c=textscan(f , '%s%s%s%f' , 'Headerlines' , 1 , 'delimiter' , ' ');
fclose(f);
t =[diff(c{end})~=0;true];
C = [c{1:3}];
data = [C(t,:),num2cell(c{end}(t))];
clearvars -except data
%combine column date and time
day = data(1:end,2);
time = data(1:end,3);
ns = datenum(day, 'MM/dd/yyyy') + datenum(time, 'hh:mm:ss') - datenum('00:00:00','hh:mm:ss');
data=[data num2cell(ns)];
data(:,2:3)=[];
%data = cell2table(data,'VariableNames',{'Symbol','Price','DateTime'});
DTn = data(:,2);
ti = 1/(60/30 * 24); % Time Interval
DTiv = transpose(DTn{1}:ti:DTn{end}); % Interpolation Vector
Price = data(:,2); % Vector: Column #2 Of Table1
DT30 = interp1({DTn}, Price, {DTiv}); % Interpolated Column #2
NewTable1 = {datestr(DTiv, 'MM/dd/yyyy hh:mm:ss') DT30};
Result = [NewTable1{1} repmat(' ', size(NewTable1{2})) num2str(NewTable1{2}, '%.2f')];
Result5 = Result(1:5,:);
The error:
% Error using interp1 (line 109)
X must be a vector of numeric coordinates.
Error in Interval30minute (line 24) DT30 = interp1({DTn}, Price, {DTiv}); % Interpolated Column #2
Attached is file using this code.
0 个评论
采纳的回答
KSSV
2016-6-15
%%reset
clear all;
close all;
clc;
%delete NaN, continuously duplicated value and keep the last one
f=fopen('CLF1997.txt');
c=textscan(f , '%s%s%s%f' , 'Headerlines' , 1 , 'delimiter' , ' ');
fclose(f);
t =[diff(c{end})~=0;true];
C = [c{1:3}];
data = [C(t,:),num2cell(c{end}(t))];
clearvars -except data
%combine column date and time
day = data(1:end,2);
time = data(1:end,3);
ns = datenum(day, 'MM/dd/yyyy') + datenum(time, 'hh:mm:ss') - datenum('00:00:00','hh:mm:ss');
data=[data num2cell(ns)];
data(:,2:3)=[];
%data = cell2table(data,'VariableNames',{'Symbol','Price','DateTime'});
DTn = data(:,2);
ti = 1/(60/30 * 24); % Time Interval
DTiv = transpose(DTn{1}:ti:DTn{end}); % Interpolation Vector
Price = data(:,2); % Vector: Column #2 Of Table1
% Convert cell to matrix
DTn = cell2mat(DTn) ;
Price = cell2mat(Price) ;
% Arrange the matrix in order
[DTn,idx] = sort(DTn) ;
Price = Price(idx) ;
% Remove doubles
[DTn1,idx] = unique(DTn) ;
DTn = DTn1 ;
Price = Price(idx) ;
DT30 = interp1(DTn, Price, DTiv); % Interpolated Column #2
NewTable1 = {datestr(DTiv, 'MM/dd/yyyy hh:mm:ss') DT30};
Result = [NewTable1{1} repmat(' ', size(NewTable1{2})) num2str(NewTable1{2}, '%.2f')];
Result5 = Result(1:5,:);
更多回答(1 个)
aastha gupta
2019-10-25
please help me with the code.I am getting an error in linear interpolation.
Error using interp1 (line 109)
X must be a vector of numeric coordinates.
V(1)= f(0)/b(0);
V(N+1) = f(1)/b(1);
W(1) = U(1) - V(1);
W(N+1) = U(N+1)-V(N+1);
for i = 2:N
V(i) = f(x(i))./b(x(i));
W(i) = U(i) - V(i);
end
backdW(2)= (W(2)-W(1))/h;
fordW(2)= (W(3)-W(2))/h;
backdW(N)= (W(N)-W(N-1))/h;
fordW(N)= (W(N+1)-W(N))/h;
secdW(2)= (fordW(2) - backdW(2))/h;
secdW(N)= (fordW(N) - backdW(N))/h;
for i = 3:N-1
backdW(i)= (W(i)-W(i-1))/h ;
fordW(i)= (W(i+1)-W(i))/h ;
secdW(i)= (fordW(i)-backdW(i))/h ;
end
avgsecdW(2)=secdW(2);
avgsecdW(N+1)=secdW(N);
for i=3:N
avgsecdW(i)= (secdW(i)+ secdW(i-1))/2 ;
end
for i=1:N
K = cumsum(h*sqrt(abs(avgsecdW(i+1))));
end
Q(2) = K + sqrt(abs(avgsecdW(2)));
Q(N+1) = K + sqrt(abs(avgsecdW(N+1)));
for i=3:N
Q(i)= K + sqrt(abs(avgsecdW(i)));
end
M(2) = h*Q(2);
for j = 3:N+1
for i = 2:j
M(j)= cumsum(h* Q(i));
end
end
Iteration = 0;
while( max(h*Q(i))/M(N+1)) > (2/N)
Iteration = Iteration + 1;
Y = linspace(0, M(N+1), N+1)';
x(1)=0; x(N+1)=1; % avoid issues with round-off
x = interp1(M, x, Y);
M(1)=0;
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
