필터 지우기
필터 지우기

How to use interp1() on table?

조회 수: 6 (최근 30일)
Trung Hieu Le
Trung Hieu Le 2016년 6월 12일
편집: Stephen23 2016년 6월 13일
Dear Everyone! I'm trying to run the "Interval30minute.m" for tick data each 30 minutes:
%%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 = DTn(1):ti:DTn(end); % Interpolation Vector
Price = data(:,1); % 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')];
However, the code was run to
DTiv = DTn(1):ti:DTn(end);
I got the error:"You can not subscript a table using only one subscript. Table subscripting requires both row and variable subscripts."
Could you please help me recommend any solution for this situation?
Thanks a lot for your help

답변 (1개)

Guillaume
Guillaume 2016년 6월 13일
Since data is a table, so is data(:, 2). To get the array stored in that column:
DTn = data{:, 2};
%or
DTn = data.Price;
The rest of the code should then work.
  댓글 수: 2
Trung Hieu Le
Trung Hieu Le 2016년 6월 13일
Hi Guillaume,
I tried with DTn = data{:,2};, however, the output is only one value. I also tried to change the below code:
% 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,:);
I faced new 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
>> RawRVofnormalandlogreturns
Operands to the || and && operators must be convertible to logical scalar values.
Error in RawRVofnormalandlogreturns (line 31)
if adata{i,2}>0.4444 && adata{i,2}<0.7431; %Boundaries in matlab format
Guillaume
Guillaume 2016년 6월 13일
편집: Guillaume 2016년 6월 13일
"I tried with DTn = data{:,2};, however, the output is only one value." Using the code and data you've provided, if I just replace the line
DTn = data(:,2);
by
DTn = data{:, 2};
then DTn is the content of the second column.
Your code then fails on interp1 because DTiv is empty. It's empty because DTn(1) is greater than Dtn(end) but it's nothing to do with your original problem.
Note that later on, you have
Price = data(:, 1);
This should be
Price = data{:, 1};
but of course, according to your headers, Price is actually the second column, so that'd be DTn. I have no idea what you're trying to do there.
Note that I'm not sure why you're converting your cell array to a table, since you're not using any of the table syntax. The above would work just the same without the cell2table call. In table syntax the two offending lines would be:
DTn = data.Price; %!
Price = data.Symbol; %!!
which clearly shows that's something is not right with your code

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

카테고리

Help CenterFile Exchange에서 Stochastic Differential Equation (SDE) Models에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by