MATLAB Answers

Classify imported files from multiple folders into cell array

2 views (last 30 days)
Tessa Kol
Tessa Kol on 13 Oct 2020
Commented: Tessa Kol on 13 Oct 2020
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);
% 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)];
k = k + 1;
However, I want to structure this cell array into a cell array of 1x14:
Cell 1:
Folder 1
Cell 2:
Folder 2
Cell 13:
Folder 6
Cell 14:
Folder 6

Answers (1)

Sudhakar Shinde
Sudhakar Shinde on 13 Oct 2020
You could try this example.
load test.mat;
name = {};
for i=1:6
Data{i} = name(contains(folder, ['test\',num2str(i)]));
The resulted output in cell:
Folder1 contains 7 data, folder2 contains 8 data etc.
Below snap shows folder1 data:

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!