Creating cell array from multiple structs that has struct components directly accessible?

7 vues (au cours des 30 derniers jours)
MATLAB beginner here so apologies if this is a basic question. I'm using Fieldtrip for EEG analysis (https://www.fieldtriptoolbox.org/tutorial/eventrelatedstatistics/) and need to create a cell array from patient data from multiple patients. Each patient in the cell array is represented by a 1x1 struct, and the data in the struct consits of 7 variables of various types (struct, double, etc.)
I'm trying to create a for loop that goes through each patient's data and aggregates it into one cell array. I was able to create a for loop that takes each patient's individual 1x1 struct and aggregates it so that I get a cell array of n 1x1 structs that contains each respective patient's data which appears to be the general right format that is needed.
For comparison to what the ideal format of the cell array is supposed to look like, I downloaded the example subject average data from the link above (allsubjFIC and allsubjFC data that can be downloaded from the hyperlinked 'subject data' text in the instructions). The cell array that they have looks very similar to mine, but they don't have the "intermediate" struct within it.
i.e. when I click through my cell array, I double click on the cell array to see its components (which is comprised of n 1x1 n structs for n patients) --> when i double click on an individual 1x1 struct it brings me to another 1x1 struct with it's specific name (e.g. timelock_2_OFF_art) --> double click on this and then it shows me the 7 variables within.
When I click on the example cell arrays (e.g. allsubjFIC), everything is essentially the same, except when I click on the original 1x1 struct representing the patient, it takes me directly to the 7 component variables, instead of to another 1x1 struct with the specific name of the original struct that was needed in the for loop.
Is there a way to "remove" the intermediate struct (or whatever it's called) so that when I double click on my cell array, it brings me to a row of 1x1 structs (each cell represents a different patient), and when I click on a given 1x1 struct, it takes me directly to the 7 component parts of that struct, instead of to another 1x1 struc that is simply a named struct that I used to store the data contained within it? I can't run any of the statistical functions in the toolbox on my cell array because it brings up the following error messages (copy and pasted below) and I think it has to do with the way I've set up the cell array.
Thanks!
allsubj_timelock_OFF_art = [];
index = [2:7 9:14]
for ii = index
timelocked = load(['timelock_' num2str(ii) '_OFF_art.mat'])
allsubj_timelock_OFF_art =[allsubj_timelock_OFF_art, {timelocked}];
end
%%Run ft_timelockgrandaverage to average data across all subjects
cfg = [];
grandavg_OFF_art_2 = ft_timelockgrandaverage(cfg, allsubj_timelock_OFF_art{:})
Error message:
Error using ft_checkdata
This function requires timelock data as input, see ft_datatype_timelock.
Error in ft_timelockgrandaverage (line 99)
varargin{i} = ft_checkdata(varargin{i}, 'datatype', 'timelock', 'feedback', 'no');

Réponses (1)

Stephen23
Stephen23 le 18 Fév 2023
Modifié(e) : Stephen23 le 18 Fév 2023
Your code would be simpler and more robust if you had given every structure exactly the same name. Rather than forcing meta-data into variable names, you should keep those variable names the same and store the meta-data in a variable. Forcing meta-data into variable names makes accessing your data slower and more complex.
With that out of the way...
Assuming that every MAT file contains exactly one variable:
V = [2:7,9:14];
N = numel(V);
C = cell(1,N);
for k = 1:N
F = sprintf('timelock_%d_OFF_art.mat',V(k));
C(k) = struct2cell(load(F));
end
S = [C{:}] % create one structure array

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by