Separating and splitting table by numeric values and writing to multiple files

I have a single CSV with data uploaded from multiple IOT sensors (all within the same file), each device's row distinguished by a unique numeric ID. I convert this into a MATLAB table using the
readtable()
method. I can then organize the file by grouping device ID's separately by using the
sortrows()
method on the Device ID column.
However, how would I split the grouped devices into seperate tables? Currently I use the following algorithm:
for g = 1:numDevices %create a new file for each device type
outputFileIndoorData1 = sprintf("C:\Users\Documents\duetTestFile%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
This is extremely resource intensive, however. What could be a more efficient way separating each devices data into a different file?

2 件のコメント

Image Analyst
Image Analyst 2022 年 8 月 29 日
編集済み: Image Analyst 2022 年 8 月 29 日
Please attach the output of sortrows as a variable in a .mat file so we can try things with it. Make it easy for people to help you, not hard.
In the meantime, see findgroups
Farid Barak
Farid Barak 2022 年 8 月 29 日
編集済み: Farid Barak 2022 年 8 月 29 日
@Image Analyst The output is too big to paste here, and is irrelevant. I've already explained what is in the file, I only need to seperate the table now based on the (sorted) values of column n.

サインインしてコメントする。

 採用された回答

dpb
dpb 2022 年 8 月 29 日
編集済み: dpb 2022 年 8 月 29 日

1 投票

" split the grouped devices into seperate tables?"
The short answer is "you don't!"
Use the DeviceID variable as a <GroupingVariable> and rowfun or groupsummary and friends to operate on those by group.
If you make more tables or files instead, you've just compounded the problem of analysis by having to bring all those back in again as they're needed when you've already got them and all the tools are already builtin with the table data class -- use them.

2 件のコメント

Farid Barak
Farid Barak 2022 年 8 月 29 日
編集済み: Farid Barak 2022 年 8 月 29 日
@dpb The groupsummary solved all of the problems I was having, thanks!
dpb
dpb 2022 年 8 月 30 日
Glad to help -- and to introduce another to the wonders of grouping variables!! :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeDevelop Apps Using App Designer についてさらに検索

質問済み:

2022 年 8 月 29 日

コメント済み:

dpb
2022 年 8 月 30 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by