Make a 3D matrix*****SOS******

I wanna make a 3D matrix (30800,4,12)

each of 12 pages which is the number of my subjects is not continuous to load them with a for loop. The name of them is specific.

I’ve used your code that was above, I cannot load all the .mat files.

Could you please help me with this1 issue?

What kind of files are those (CSV, XLS, etc.)? Also I do not see any code above, may you share what you have tried again?

Thx in advance for your response.
the format is ‘’.mat’’
I just do not know how to load each of them and merge in a 3D matrix

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.

Steven

Thanks a billion. Due to I am a beginner in coding. would you please help me more?

I can share my email with you to be in contact.
nedadel1995@gmail.com

I am happy to help more here. What kind of data are in each .mat file?

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

files = dir
n_files = size(files,1)
concatenated_matrix = [] 
for i=1:n_files
    file_to_load = files(i).name
    data_loaded = load(file_to_load)
    concatenated_matrix = cat(3, concatenated_matrix, data_loaded)
end

It is the whole code which works in my case?

Try it out for yourself and see if it works. I don’t have your data, but hopefully no or few changes are needed to get it to work for you.

I should write the address of data in front of ‘‘dir’’?

Many thanks dear Steven for your help.
If I have further questions, what should I do

It would be easier to just get MATLAB to directory where the files are (either by clicking in the navigation window or by using the cd command).

You can reply here. Good luck!

Steven

thanks a lot for your help.

Hi Steven,
I use this code

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?

Steven

1 Like

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.

https://drive.google.com/drive/folders/1zpcYCHZh1Y9UExxafpGe_duuxryZGb_R?usp=sharing

Many Thanks for your time & help.

MANAGED BY INCF