Clear Filters
Clear Filters

Organizing Data from a Cell Array

4 views (last 30 days)
Zachary Eisner
Zachary Eisner on 13 Apr 2021
Answered: Jaynik on 28 Feb 2024
I have a cell array from an experiment with the following structure:
allFileInfo =
1×30 cell array
Columns 1 through 6
{42×1 struct} {42×1 struct} {42×1 struct} {42×1 struct} {42×1 struct} {56×1 struct}
Columns 7 through 12
{42×1 struct} {56×1 struct} {56×1 struct} {56×1 struct} {56×1 struct} {56×1 struct}
Columns 13 through 18
{56×1 struct} {56×1 struct} {56×1 struct} {56×1 struct} {56×1 struct} {56×1 struct}
Columns 19 through 24
{56×1 struct} {56×1 struct} {56×1 struct} {56×1 struct} {56×1 struct} {56×1 struct}
Columns 25 through 30
{56×1 struct} {56×1 struct} {56×1 struct} {56×1 struct} {56×1 struct} {56×1 struct}
Each structure represents a different subject, and there are 56 conditions per subject (the first five subjects only participated in 42 conditions)
Each of the internal structures looks like the following:
allFileInfo{1} =
42×1 struct array with fields:
name
folder
date
bytes
isdir
datenum
fullfile
data
Where each of the 42 elements is extracted from a text file. Really the only fields I care about here are "fullfile", the directory of the text file, and "data", a nx2 table with voltage values over time. I am looking to categorize these tables into new cell arrays based on the "fullfule" string. which indicates the conditions under which each measurement was taken. For context, each subject was measured from the LEFT and RIGHT, under four conditions: NOTHING, COLLAR, FOAM, TOWEL, and in seven directions: BASELINE, LTILT, RTILT, FOREWARD, BACKWARD, RROT, LROT. The name of each file looks like the following, noting the conditions accordingly:
allFileInfo{1}(1).fullfile =
'***/Subject 1/Left/Collar/Backward.txt'
So in total, there are 1596 files, and I would like to group them into like conditions, rather than by subject. The end result would look something like a cell array where all LEFT, NOTHING, BASELINE tables are together, so there would be 56 seperate structures for the 56 combinations of different conditions. In theory I know how to make this work, but Im not sure about how to do the grouping based on the "fullfile" string. Any help is much appreciated. Thanks!

Answers (1)

Jaynik
Jaynik on 28 Feb 2024
Hi Zachary,
Assuming that the "fullfile" string follows the same format for all the conditions, you can use "strsplit" to obtain the conditions. Then you can utilize the "containers.Map" object to group the data based on these conditions. Following is a sample code that does the grouping:
% Initialize a map from string to any
finalData = containers.Map('KeyType', 'char', 'ValueType', 'any');
% Loop for subject
for i = 1:length(allFileInfo)
% Loop for condition
for j = 1:length(allFileInfo{i})
filepath = allFileInfo{i}(j).fullfile;
dataTable = allFileInfo{i}(j).data;
% Split the file path into parts based on / using strsplit
pathParts = strsplit(filepath, '/');
% Assuming the filepath is consistent
subjectPart = pathParts{end-3};
sidePart = pathParts{end-2};
conditionPart = pathParts{end-1};
directionPart = strrep(pathParts{end}, '.txt', ''); % Removing extension
% Create a unique key for the group
groupKey = sprintf('%s_%s_%s', sidePart, conditionPart, directionPart);
if isKey(finalData, groupKey)
finalData(groupKey) = [finalData(groupKey); {dataTable}];
else
finalData(groupKey) = {dataTable};
end
end
end
% Convert the map to a cell array with the all the values
finalDataCellArray = values(finalData);
You can refer to the following documentation to read about each of the following:

Categories

Find more on Structures in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!