How to import data from different sessions of an experiment within the same script?

2 ビュー (過去 30 日間)
Steve Schulz
Steve Schulz 2021 年 12 月 7 日
回答済み: Vatsal 2024 年 2 月 26 日
Hey there,
I'm currently conducting a study regarding memory capability. The study is designed as a within-subject experiment. Participants have to complete two sessions (on two seperate days). The data is located in two seperate folders for each participant. Now I try to find a way to import the data from both sessions into one script to perform analysis separately (for each session) and then combine these results.
The way is was doing it so far is the following (importing data from session 1, the same for session 2):
subfiles = dir('../Desktop/results/sourcedata/participant-3*/ses-1/behav/*task-memory.mat')
subjects = {}
if isempty(subjects)
% select all in subdir
subjects = NaN(1,length(subfiles));
for i = 1:length(subfiles)
subjects(i) = str2double(subfiles(i).name(end-22:end-20));
end
end
results = struct();
for subjn = 1:length(subjects)
if subjn <= length(subjects)
%% get the subjects data
temp_data_bundle = load(fullfile(subfiles(subjn).folder, subfiles(subjn).name))
end
end
for subjn = 1:length(subjects)
if subjn <= length(subjects)
temp_data_bundle = load(fullfile(subfiles(subjn).folder, subfiles(subjn).name))
else
temp_data = results(end).subdata.data;
end
results(subjn).name = temp_data_bundle.parameters.subject_nr;
I'm using an existing script which is shared in my workgroup and adding my specific analysis to it. I know that there are various ways to import data but which one might be suitable for me?
I appreciate any advice. Thanks!

回答 (1 件)

Vatsal
Vatsal 2024 年 2 月 26 日
Hi,
The script is using the 'dir' function to find all the .mat files in a directory for each session. After that, it is using 'load' to open each file. To manage data from more sessions, the script can be updated to loop through the sessions.
sessions = 1:2; % number of sessions
subjects = {}; % initialize subjects
results = struct(); % initialize results
for s = sessions
subfiles = dir(['../Desktop/results/sourcedata/participant-3*/ses-' num2str(s) '/behav/*task-memory.mat']);
if isempty(subjects)
subjects = NaN(1,length(subfiles));
for i = 1:length(subfiles)
subjects(i) = str2double(subfiles(i).name(end-22:end-20));
end
end
for subjn = 1:length(subjects)
if subjn <= length(subjects)
temp_data_bundle = load(fullfile(subfiles(subjn).folder, subfiles(subjn).name));
results(subjn).name = temp_data_bundle.parameters.subject_nr;
results(subjn).session(s).data = temp_data_bundle; % store data for each session
end
end
end
I hope this helps!

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by