Info
此问题已关闭。 请重新打开它进行编辑或回答。
Different result while run the same code? longer space between 2 column
3 次查看(过去 30 天)
显示 更早的评论
Hi everyone! Could you help me review my code why I got different results when I run the same code? Frist file with the result:
val =
12/27/2007 22:00:00 87.01
12/27/2007 22:30:00 87.01
12/27/2007 23:00:00 87.02
12/27/2007 23:30:00 87.02
12/28/2007 00:00:00 87.03
Second file with the result:
val =
08/29/1996 15:30:00 17.85
08/29/1996 16:00:00 17.85
08/29/1996 16:30:00 17.85
08/29/1996 17:00:00 17.85
08/29/1996 17:30:00 17.85
The first result has the longer space than the second result. It made a difficult for me to run next codes. I wonder why it has a difference like that? The input file has the same format. The code used as follows:
%%reset
clear all;
close all;
clc;
%delete NaN, continuously duplicated value and keep the last one
f=fopen('CLF1999.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(:,1:3)=[];
%data = cell2table(data,'VariableNames',{'Symbol','Price','DateTime'});
DTn = data(:,2);
sortminute = minute(datetime(DTn{1},'ConvertFrom','datenum'));
if(sortminute>=30)
firstdata = dateshift(datetime(DTn{1},'ConvertFrom','datenum'),'start','hour') + minutes(60);
else
firstdata = dateshift(datetime(DTn{1},'ConvertFrom','datenum'),'start','hour') + minutes(30);
end
ti = 1/(60/30 * 24); % Time Interval
DTiv = [datenum(firstdata):ti:DTn{end}]'; % Interpolation Vector
Price = data(:,1); % 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};
CLF1999 = [NewTable1{1} repmat(' ', size(NewTable1{2})) num2str(NewTable1{2}, '%.2f')];
Thanks a lot for your help.
1 个评论
Geoff Hayes
2016-6-19
Trung - where does the val variable get set in your above code? Please clarify.
回答(0 个)
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!