在Matlab上进行数据存储和处理大数据

时间:2018-11-16 15:25:40

标签: matlab multiple-columns

我有一个23k的逐笔逐笔股票市场数据的大数据集,没有以变量名作为头且没有定界符。请查看图片以了解我的数据集。

数据必须分成具有变量名称和长度的列,其长度指定如下

  • 变量1:Mkt,长度:2个字节
  • 变量2:段,长度:4个字节
  • 变量3:订单编号,长度:16个字节
  • 变量4:时间,长度:14个字节
  • 变量5:BSI,长度:1个字节
  • 变量6:活动,长度:1个字节
  • 变量7:符号,长度:10个字节
  • 变量8:系列,长度:2个字节
  • 变量9:卷,长度:8个字节
  • 变量10:VolOrg,长度:8个字节
  • 变量11:Ltp,长度:8个字节
  • 变量12:Tp,长度:8个字节
  • 变量13:MOF,长度:1个字节
  • 变量14:DD,长度:1个字节
  • 变量15:FEM,长度:1个字节
  • 变量16:Ind,长度:1个字节
  • 变量17:Ins,长度:1个字节

如何将单个列拆分为上述指定列数并在 Matlab Datastore 中处理以提取一些必要的信息?到目前为止,我找不到任何资源。

这是Matlab数据存储库预览命令的屏幕截图: screenshot of Matlab datastore preview command

1 个答案:

答案 0 :(得分:0)

您将需要一个自定义函数来读取列分隔的文本文件。这样做的方法很多,有些效率比其他方法高得多。 据我所试验,sscanf是将大量文本转换为数值的最有效函数,尽管它要求将值用空格分隔(这需要一些选择和填充)。以下代码应该正常工作,并且可以尽快完成,而无需用C重写代码的某些部分。

function data=customRead(filename)

%% Define the file format there %%
format = {'Mkt',     [1 2],  'str';...
          'Seg',     [3 6],  'str';...
          'OrderNo', [7 22], 'int64';...  %(64 bit because int32 has 9 digits max)
          'Time',    [23 36], 'int64';...
          'BSI',     [37 37], 'int'}; %The same goes on for other fields

%% Load the file content in memory %%
fid=fopen(filename,'r');
allData=fread(fid,Inf,'*uint8');
fclose(fid);

%% Make it an array, with one line from the dat file in each row %%
bytesPerLine = 87; % 86 bytes + 1 newline (or is it 2 for \r\n ?)
nLines = length(allData)/bytesPerLine;
allData=char(reshape(allData,[bytesPerLine,nLines])'); %Each line of the input is now stored in a row

%% Loop over format fields and populate the output structure
for ii=1:size(format,2)
   thisData= allData(format{ii,2},:); %select range from char array
   switch format{ii,3}
       case 'str'
           % cast to string
           data.(format{ii,1})=string(thisData);
       case 'int'
           thisData = [thisData,repmat(' ',[nLines,1])]'; %pad with a space after each value and transpose
           thisData = thisData(:); %make it a vector
           data.(format{ii,1})=int32(sscanf(thisData,'%d')); %read with sscanf (this is fast)
       case 'int64'
           thisData = [thisData,repmat(' ',[nLines,1])]';
           thisData = thisData(:)';
           data.(format{ii,1})=sscanf(thisData,'%li'); % %li to read as int64 
       otherwise
           error('%s : unknown format %s',format{ii,1},format{ii,3});
       end
   end
end

结果是一个结构,每个字段都是 string int32 int64

的列向量

然后您可以使用自定义函数定义数据存储:

store = fileDatastore(location,'ReadFcn',@customRead);
相关问题