How to use interp1() on table?
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Antworten (1)
Guillaume
am 13 Jun. 2016
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 Kommentare
Guillaume
am 13 Jun. 2016
Bearbeitet: Guillaume
am 13 Jun. 2016
"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
Siehe auch
Kategorien
Mehr zu Stochastic Differential Equation (SDE) Models finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!