按数值分隔和拆分表,并写入多个文件

  • 本文关键字:文件 分隔 拆分 matlab
  • 更新时间 :
  • 英文 :


我有一个CSV,其中包含从多个IOT传感器上传的数据(均在同一文件中(,每个设备的行由一个唯一的数字ID区分。我使用readtable()方法将其转换为MATLAB表。然后,我可以通过使用"设备ID"列上的sortrows()方法将设备ID单独分组来组织文件。

但是,如何将分组的设备拆分为单独的表?目前我使用以下算法:

for g = 1:numDevices %create a new file for each device type
outputFileIndoorData1 = sprintf("C:UsersDocumentsduetTestFile%d",g);
for h = 1:height(indoorDataSorted) %Iterate through entire CSV, splitting by device ID
if strcmp(indoorDataSorted.device_id(h),deviceIDs(g,1)) 
writelines(indoorDataSorted(h,1:17),outputFileIndoorData1); %Write to individual file specified
end
end
end 

然而,这是极其耗费资源的。有什么更有效的方法可以将每个设备的数据分离到不同的文件中?

使用基于unique函数的几行代码,您应该能够非常高效地完成这项工作。

%Setup a small sample data table to work with.
exampleData = cell2table({...
'd1'  1   2; ...
'd1'  3   4; ...
'd1'  5   6; ...
'd1'  7   8; ...
'd2'  9   10; ...
'd2'  11  12; ...
'd3'  13  14; ...
'd3'  15  16; ...
'd3'  17  18; ...
'd3'  19  20; ...
}, 'VariableNames', { ...
'DeviceName', 'data1', 'data2'} );
% The "unique" built-in function is pretty efficient, and 
%     outputs some useful secondary outputs.  We're going to use
%     the 3rd argument, that I have names ixs2
[deviceNames, ixs1, ixs2] = unique(  exampleData.DeviceName  );
%Now, based on the "deviceNames", and "ixs2" output, we can just loop
%    through and save output    
for ixDevice = 1:length(deviceNames)
curDevice = deviceNames{ixDevice};
curMask = (ixs2 == curDevice);

curData = exampleData(curMask,:);

%Save data here. Save the whole thing at once.
%    Name, if needed, is: curDevice
%    Datatable is: curData

end

对于任何没有在侧边运行实时版本的Matlab的人,在这种情况下unique调用的输出如下:

%The standard output, a list of unique names
deviceNames =
3×1 cell array
{'d1'}
{'d2'}
{'d3'}
%A set of indexes, which point from the original set into the new set.
%   Strictly speaking, this doesn't have to be unique. But the function
%   always points to the first one.
ixs1 =
1
5
7
%A set of indexes, which map each element of the original set to the 
%    unique set from output argument #1. This is often the most 
%    useful output. This question is a decent example. It can also
%    be used as the fist input to an "accumarray" function call, which
%    can be incredibly powerful.
ixs2 =
1
1
1
1
2
2
3
3
3
3

相关内容

  • 没有找到相关文章

最新更新