Classify imported files from multiple folders into cell array

1 回表示 (過去 30 日間)
Tessa Kol
Tessa Kol 2020 年 10 月 13 日
コメント済み: Tessa Kol 2020 年 10 月 13 日
Dear all,
I have multiple files located in multiple folders. De main folder I called 'test'. De main folder contains 6 subfolders. In each subfolder there are multiple files. I open all the files with the following line of code:
files = dir(fullfile(uigetdir,'\**\*.data*'));
The result is attached to this post (test.mat). In total I have 53 .data files.
With the code below I can import the data of every .data file and put it into a seperate cell. I thus get a cell array of 1x53 for runData, expData and velData.
rhoPart = 2314.3;
files = dir(fullfile(uigetdir,'\**\*.data*'));
k = 1;
for i = 1:numel(files)
fid = fopen(fullfile(files(i).folder,files(i).name),'r');
%% Reading the data
% Read all the data from the file
dataRead = textscan(fid,'%f %f %f %f %f %f %f %f %f %f %f %f %f %f','HeaderLines',1);
frewind(fid);
% Read the headerline from the file
Headerline = textscan(fgetl(fid),'%f %f %f %f %f %f %f %f %f','HeaderLines',0);
% Write headerline N, time, xmin, ymin, zmin, xmax, ymax, zmax
runData{k} = [Headerline{1}(:) Headerline{2}(:) Headerline{3}(:) Headerline{4}(:) Headerline{5}(:) Headerline{6}(:) Headerline{7}(:) Headerline{8}(:) Headerline{9}(:)];
% Write only the x, y, and z components of the particles, particle radius, z component+ particle radius and volume of the particle
expData{k} = [dataRead{1}(:,1) dataRead{2}(:,1) dataRead{3}(:,1) dataRead{7}(:,1) dataRead{3}(:,1)+dataRead{7}(:,1) rhoPart*(4/3)*pi*(dataRead{7}(:,1).^3)];
% Write only the vx,vy,vz of the particles and magnitude
velData{k} = [dataRead{4}(:,1) dataRead{5}(:,1) dataRead{6}(:,1) sqrt(dataRead{4}(:,1).^2 + dataRead{5}(:,1).^2 + dataRead{6}(:,1).^2)];
fclose(fid);
k = k + 1;
end
However, I want to structure this cell array into a cell array of 1x14:
Cell 1:
Folder 1
siloMultiParamMPIset1.1.data.0 siloMultiParamMPIset1.1.data.1 siloMultiParamMPIset1.1.data.2 siloMultiParamMPIset1.1.data.3 siloMultiParamMPIset1.1.data.15 siloMultiParamMPIset1.1.data.16 siloMultiParamMPIset1.1.data.17
Cell 2:
Folder 2
siloMultiParamMPIset1.1.data.0 siloMultiParamMPIset1.1.data.2 siloMultiParamMPIset1.1.data.3 siloMultiParamMPIset1.1.data.4 siloMultiParamMPIset1.1.data.5 siloMultiParamMPIset1.1.data.26 siloMultiParamMPIset1.1.data.27
...
Cell 13:
Folder 6
siloMP1DV1_muSP_0.7684_muRP_0.4443.4.data.1596 siloMP1DV1_muSP_0.7684_muRP_0.4443.4.data.1597
Cell 14:
Folder 6
siloMP1DV1_muSP_0.7987_muRP_0.3228.5.data.1264 siloMP1DV1_muSP_0.7987_muRP_0.3228.5.data.1265 siloMP1DV1_muSP_0.7987_muRP_0.3228.5.data.1266

回答 (1 件)

Sudhakar Shinde
Sudhakar Shinde 2020 年 10 月 13 日
You could try this example.
load test.mat;
folder={files.folder};
name = {files.name};
for i=1:6
Data{i} = name(contains(folder, ['test\',num2str(i)]));
end
The resulted output in cell:
Folder1 contains 7 data, folder2 contains 8 data etc.
Below snap shows folder1 data:
  2 件のコメント
Tessa Kol
Tessa Kol 2020 年 10 月 13 日
編集済み: Tessa Kol 2020 年 10 月 13 日
I think that structering the files has to be done after opening all the files. I already managed to structure the data to there corresponding folder. With the following code:
%% Loading the data
rhoPart = 2314.3;
files = dir(fullfile('C:\Users\tvvan\Desktop\test','\**\*.data*'));
folders = {files.folder};
folder_groups = findgroups(folders);
% Read only the files every 0.02 seconds approximitaly, which corresponds to the time increment of the experiment
k = 1;
for i = 1:numel(files)
fid = fopen(fullfile(files(i).folder,files(i).name),'r');
%% Reading the data
% Read all the data from the file
dataRead = textscan(fid,'%f %f %f %f %f %f %f %f %f %f %f %f %f %f','HeaderLines',1);
frewind(fid);
% Read the headerline from the file
Headerline = textscan(fgetl(fid),'%f %f %f %f %f %f %f %f %f','HeaderLines',0);
% Write headerline N, time, xmin, ymin, zmin, xmax, ymax, zmax
runData{k} = [Headerline{1}(:) Headerline{2}(:) Headerline{3}(:) Headerline{4}(:) Headerline{5}(:) Headerline{6}(:) Headerline{7}(:) Headerline{8}(:) Headerline{9}(:)];
% Write only the x, y, and z components of the particles, particle radius, z component+ particle radius and volume of the particle
expData{k} = [dataRead{1}(:,1) dataRead{2}(:,1) dataRead{3}(:,1) dataRead{7}(:,1) dataRead{3}(:,1)+dataRead{7}(:,1) rhoPart*(4/3)*pi*(dataRead{7}(:,1).^3)];
% Write only the vx,vy,vz of the particles and magnitude
velData{k} = [dataRead{4}(:,1) dataRead{5}(:,1) dataRead{6}(:,1) sqrt(dataRead{4}(:,1).^2 + dataRead{5}(:,1).^2 + dataRead{6}(:,1).^2)];
fclose(fid);
k = k + 1;
end
%% Classify (into a structure)
runData_struc = splitapply(@(x) {x}, runData, folder_groups);
expData_struc = splitapply(@(x) {x}, expData, folder_groups);
velData_struc = splitapply(@(x) {x}, velData, folder_groups);
Then I get the following:
However, this is not exactly what I wanted. I wanted to have a 1x14 cell array. Because folder 5 and 6 contain 5 series of files, thus every serie needs a separate cell. And the files in the folder are not imported into natural order.
Tessa Kol
Tessa Kol 2020 年 10 月 13 日
I want something like in the figure below:

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

カテゴリ

Help Center および File ExchangeGas Dynamics についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by