Storing a table in a struct in embedded loops
23 ビュー (過去 30 日間)
古いコメントを表示
Hello! I am trying to loop through 30 participants' data where each participant has 5 sessions associated with it. I have figured out a way to successfully access the tables I am interested in working with but having trouble storing them. I would like to store them in a struct that has 30 fields, one for each participant and then 5 session fields within each participant field. The table, as an array (see underlined line) would then be stored in the session field. The addressing in the underlined line is where I keep getting messed up. I got it to work as "DATA.i.j" but the i and j would overwrite and not change with the loop to be DATA.1.1, DATA.1.2,.. DATA.1.5, DATA.2.1, etc. This is the desired outcome.
I tried using the names from the cell array for the session names and the field names for the participants, as you can see here, so that it would be DATA.P1.Session 1 but frankly the example I described previously in bold is the ideal.
Is there any way to pull this off?
%load the field names of the participants
load("fn.mat")
%make a cell array of session folder names
sn = {'Session 1','Session 2','Session 3','Session 4', 'Session 5'};
DATA = struct([]);
for i = 1: numel(fn)
foldername = fn{i};
for j = 1:numel(sn)
sessionName = sn{j};
Filename = fullfile('Users','emmawalker','Decision Engineering', 'ProjLong',
'PreProcessed',sessionName,foldername,'Hb_trigger_index.csv');
data = readtable(Filename);
DATA.(fn{i}).(sn{j}) = table2array(data);
end
end
0 件のコメント
回答 (1 件)
Walter Roberson
2023 年 9 月 20 日
DATA is a struct, so dynamic field names for it must obey the limitations for struct field names. struct field names must be valid MATLAB variable names -- must start with an upper or lower case English letter, followed by up to 62 more characters that are upper or lower case English letters or are the digits 0 to 9 or are the underscore ("_") character.
You are using (sn{j}) as a dynamic field name in the struct. But your sn contain spaces. Spaces are not valid in MATLAB variable names, so they are not valid in struct field names.
The rules are different for variable names for columns of table objects. It is generally valid to have a space in a column name for table objects.
3 件のコメント
Walter Roberson
2023 年 9 月 20 日
As I indicated, field names must begin with an upper case or lower case English letter. DATA.1.5 has an invalid field name of "1"
What you could do would be something like
%load the field names of the participants
load("fn.mat")
%make a cell array of session folder names
sn = {'Session 1','Session 2','Session 3','Session 4', 'Session 5'};
snfields = matlab.lang.makeValidName(sn);
DATA = struct([]);
for i = 1: numel(fn)
foldername = fn{i};
for j = 1:numel(sn)
sessionName = sn{j};
Filename = fullfile('Users','emmawalker','Decision Engineering', 'ProjLong', 'PreProcessed',sessionName,foldername,'Hb_trigger_index.csv');
data = readtable(Filename);
DATA.(fn{i}).(snfields{j}) = table2array(data);
end
end
参考
カテゴリ
Help Center および File Exchange で Structures についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!