matlab:合并单元格数组,转换为时间序列

时间:2012-08-29 10:21:28

标签: matlab cell-array

我正在摆弄matlab(没有添加任何工具箱),我正在尝试了解如何从交易数据库中创建x分钟栏。

从SQL服务器访问数据,我得到一个单元格数组,Buy_Data:

    Attributes: []
          Data: {619134x2 cell}
DatabaseObject: [1x1 database]
      RowLimit: 0
      SQLQuery: [1x210 char]
       Message: []
          Type: 'Database Cursor Object'
     ResultSet: [1x1 com.microsoft.sqlserver.jdbc.SQLServerResultSet]
        Cursor: [1x1 com.mathworks.toolbox.database.sqlExec]
     Statement: [1x1 com.microsoft.sqlserver.jdbc.SQLServerStatement]
         Fetch: [1x1 com.mathworks.toolbox.database.fetchTheData]

现在,内容看起来像:

'2012-08-28 16:48:56.0'    [24800]
'2012-08-28 16:48:56.0'    [24800]
'2012-08-28 16:49:14.0'    [24800]
'2012-08-28 16:49:14.0'    [24800]
'2012-08-28 16:49:21.0'    [24799]
'2012-08-28 16:49:51.0'    [24800]
'2012-08-28 16:49:51.0'    [24800]
'2012-08-28 16:49:55.0'    [24800]
'2012-08-28 16:49:55.0'    [24800]
'2012-08-28 16:49:56.0'    [24800]
'2012-08-28 16:49:56.0'    [24800]
'2012-08-28 16:49:56.0'    [24800]
'2012-08-28 16:49:56.0'    [24800]
'2012-08-28 16:49:56.0'    [24800]
'2012-08-28 17:00:12.0'    [24839]

如何将这些细胞阵列中的2个组合成x分钟的时间序列?

1 个答案:

答案 0 :(得分:1)

好吧,组合单元格就像

一样简单
new_Data = [Buy_data.Data; Buy_data2.Data]
显然,Buy_dataBuy_data2是你的两种不同结构。

将这些组合成x分钟烛台格式可以这样做:

% example data
new_cell = {
    '2012-08-28 16:48:56.0'    [24800]
    '2012-08-28 16:48:56.0'    [24800]
    '2012-08-28 16:49:14.0'    [24800]
    '2012-08-28 16:49:14.0'    [24800]
    '2012-08-28 16:49:21.0'    [24799]
    '2012-08-28 16:49:51.0'    [24800]
    '2012-08-28 16:49:51.0'    [24800]
    '2012-08-28 16:49:55.0'    [24800]
    '2012-08-28 16:49:55.0'    [24800]
    '2012-08-28 16:49:56.0'    [24800]
    '2012-08-28 16:49:56.0'    [24800]
    '2012-08-28 16:49:56.0'    [24800]
    '2012-08-28 16:49:56.0'    [24800]
    '2012-08-28 16:49:56.0'    [24800]
    '2012-08-28 17:00:12.0'    [24839]
    };

% your period (in minutes)
x = 1; 



% convert dates to numbers and make sure dates are sorted
[dateNums, sortInds] = sort(datenum(new_cell(:,1)));
prices = [new_cell{:,2}].';
prices = prices(sortInds);

% find minimum date, maximum date
minDate = min(dateNums);
maxDate = max(dateNums);

% group data into bins
x = x/24/60;
numBins = ceil( (maxDate-minDate)/x );
[N,X] = hist(dateNums,numBins);

% create candlesticks
inds          = [1 cumsum(N)];
candles.min   = zeros(numBins,1);
candles.max   = zeros(numBins,1);
candles.open  = zeros(numBins,1);
candles.close = zeros(numBins,1);
candles.dates = cell(numBins,1);
for ii = 1:numBins

    % we already know the dates
    candles.dates{ii} = datestr(X(ii));

    % continue only if there's data    
    if N(ii)
        % extract the prices
        subSet = prices(inds(ii):inds(ii+1));
        % find max, min, open, close
        candles.open(ii)  = subSet(1);
        candles.close(ii) = subSet(end);
        candles.max(ii)   = max(subSet);
        candles.min(ii)   = min(subSet);        
    end    

end

请注意,你需要做一些调整,因为这些是非常难以代表的烛台:)无论如何,它应该足以让你开始。