Calling and analyzing data from different subject folders

5 次查看(过去 30 天)
Hi,
My directory is set up that each subject # folder has folders within that coordinate with the specific task the subject has done. So the subject 555 has 3 folders and in each of the 3 folders are .mat files that are labeled year-month-day.subj#. What I want to do is go into each subject folder and task folder and load in the most recent 3 days for each subject. I then want to perform analysis on each subjects 3 days and graph it( I think I have that part already, its really the loading that is giving me trouble). My intial thought is to set which subjects and days I want and have some sort of loop that goes changes the directory path based on subject number and days. Any help would be greatly appreciated!
Thank you!

回答(1 个)

Samay Sagar
Samay Sagar 2024-2-19
To load the recent three days of data for each subject and task within your directory structure, you can create a MATLAB script that navigates through the directories, identifies the relevant MAT files, and performs your analysis
You can use the following script to navigate through the directories and load data from the recent MAT files
% Define the root directory where subject folders are located
rootDir = 'path_to_your_subjects_directory'; % e.g., 'C:/data/subjects'
% Get a list of all subject folders in the root directory
subjects = dir(rootDir);
subjects = subjects([subjects.isdir]); % Filter out non-directory entries
subjects = subjects(~ismember({subjects.name}, {'.', '..'})); % Remove '.' and '..' directories
% Loop over each subject folder
for i = 1:length(subjects)
subjectDir = fullfile(rootDir, subjects(i).name);
tasks = dir(subjectDir);
tasks = tasks([tasks.isdir]); % Filter out non-directory entries
tasks = tasks(~ismember({tasks.name}, {'.', '..'})); % Remove '.' and '..' directories
% Loop over each task folder
for j = 1:length(tasks)
taskDir = fullfile(subjectDir, tasks(j).name);
matFiles = dir(fullfile(taskDir, '*.mat'));
% Extract dates and sort them to find the most recent files
dates = datetime({matFiles.date});
[~, ind] = sort(dates, 'descend');
% Take the most recent 3 files
recentFiles = matFiles(ind(1:min(3, end)));
% Load and analyze each of the recent 3 files
for k = 1:length(recentFiles)
filePath = fullfile(taskDir, recentFiles(k).name);
data = load(filePath);
% Perform your analysis here
% ...
end
end
end
For each task, the script will sort the MAT files by date, enabling it to select the latest three files for processing.
Read more about “datetime”, “dir” and “fullfile” here:

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by