Assuming MATLAB is running in directory where those files live, here is a basic script:
files = dir
n_files = size(files,1)
for i=1:n_files
file_to_load = files(i).name
file_loaded = load(file_to_load)
%DO STUFF WITH THE LOADED FILE
end
Without knowing what kind of data are in these files, it is hard to help further, but the above will loop through the files and load them sequentially, and you can add code to append the loaded files to an output matrix or something else appropriate.
brain signal(EEG data)
each file should be a matrix like this: (30800,4,12)
which 12 is the sum of participants
the main problem is that name of them are not in order, so I cannot set up a for loop. I should load them manually and one by one, but I don’t know how!
I’m a bit confused now, is each file coming from a single subject? My understanding of your problem is that you want to load each subject’s data (30800,4) for 12 subjects and combine them to a final output of (30800,4,12). Is that not right?
you are totally right
I meant the name of them for instance P04, P06, P07 etc. is not continuously and in order to set up a loop like ‘‘i=1:12’’
Hope to say in a good way
D = ‘path to where the files are’;
S = dir(fullfile(D,’*.mat’));
N = numel(S);
C = cell(1,N);
for k = 1:N
T = load(fullfile(D,S(k).name));
C{k} = T.fieldname; % pick the fieldname to suit.
end
A = cat(3,C{:});
save(‘array.mat’,‘A’)
what should I write instead of ‘‘fieldname’’?
how can I put all variables instead?
I’ve got this error
Without having your files, it is hard to know how to index them. I was under the impression each subject file was just a 30800X4 matrix. What else do they have that needs to be saved? What does the variable T look like for a single file?
Also, is there a reason you switched to a cell for the output instead of starting with an empty array that grows with each iteration?
OOpss!
There are lots of problems!
the size of each file is not the same!! I should crop to the smallest, in this way I lose significant data points. Do you have any suggestions on how to make all sizes the same without losing data (&how)?
gonna use code below:
D = ‘’;
S = dir(fullfile(D,’*.mat’));
for k = 1:numel(S)
F = fullfile(D,S(k).name);
C = struct2cell(load(F));
S(k).data = C{1};
end
A = cat(3,S(k).data);
save(‘array.mat’,‘A’)
Is it correct?
Please correct me if I am wrong
Btw I will share the data in the link below with you.