Devide cell array into unequal length cells based on file name

With the following code I imported all of the .data files. All .data files of simulation 1 are located in folder 1 and all .data files of simulation 2 are located in folder 2.
%% Loading the data
rhoPart = 2540;
files = dir(fullfile(uigetdir,'\**\*.data*'));
[~,Index] = natsort({files.name});
files = files(Index);
expData = cell(length(files),1);
k = 1;
for i = 1:length(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);
% Write headerline N, time, xmin, ymin, zmin, xmax, ymax, zmax
runData{k} = strsplit(fgetl(fid), ' ');
% 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
This is the resulting workspace:
Simulation 1 (thus folder 1) contains 2745 .data files and simulation 2 (thus folder 2) contains 2741 files.
Is there a way to organize expData into a 2745x1 cell and 2741x1 cell without having to look up manually how many files each folder contains?

 採用された回答

Ameer Hamza
Ameer Hamza 2020 年 9 月 19 日

0 投票

Divide the data based on folder names. Something like this
%% Loading the data
rhoPart = 2540;
files = dir(fullfile(uigetdir,'\**\*.data*'));
[~,Index] = natsort({files.name});
files = files(Index);
folders = {files.folder};
folder_groups = findgroups(folders);
expData = cell(length(files),1);
k = 1;
for i = 1:length(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);
% Write headerline N, time, xmin, ymin, zmin, xmax, ymax, zmax
runData{k} = strsplit(fgetl(fid), ' ');
% 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
expData_partition = splitapply(@(x) x, expData, folder_groups);

3 件のコメント

Tessa Kol
Tessa Kol 2020 年 9 月 20 日
編集済み: Tessa Kol 2020 年 9 月 20 日
Thank you for your reply. Unfortunatly, I got the following error:
Error using splitapply (line 101)
The data variables must have the same number of columns as the vector of group numbers. The group number vector has 5486 column(s), and data
variable 1 has 1 column(s).
Error in Untitled (line 26)
expData_partition = splitapply(@(x) x, expData, folder_groups);
When I deleted the following line of code:
expData = cell(length(files),1);
I got yet another error:
Error using splitapply (line 132)
The function '@(x)x' returned a non-scalar value when applied to the 1st group of data.
To compute nonscalar values for each group, create an anonymous function to return each value in a scalar cell:
@(x){x}
Error in Untitled (line 26)
expData_partition = splitapply(@(x) x, expData, folder_groups);
Tessa Kol
Tessa Kol 2020 年 9 月 20 日
Never mind. The curly brackets were missing, thus it should be:
expData_partition = splitapply(@(x) {x}, expData, folder_groups);
Ameer Hamza
Ameer Hamza 2020 年 9 月 20 日
編集済み: Ameer Hamza 2020 年 9 月 20 日
Yes, I wrote that line based on guess of the content of expData. I am glad that it eventually worked!

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

その他の回答 (0 件)

カテゴリ

製品

質問済み:

2020 年 9 月 19 日

編集済み:

2020 年 9 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by