1
votes

I have 429 numerical matrices of identical size (107 rows by 36 columns), stored inside sequentially named .mat files (e.g: subj_00000.mat ... subj_00428.mat). Here's what I need to do:

  1. Import into the MATLAB workspace.
  2. After importing, average all of them to generate another matrix, which will also have a dimension of 107x36.
  3. Finally, linearly correlate each column of the average matrix with each column of each of the original 429 matrices, to generate a new matrix of 429 rows and 36 columns.

So far I got to the stage of building a 107 x 36 x 429 array to be filled with my set of matrices.

S = dir(fullfile(D,'subj*.mat')); % D is the path where the .mat files are saved
N = numel(S);
C = cell(1,N); % preallocate cell array
for k = 1:N
    S = load(fullfile(D,S(k).name));
    C(k) = struct2cell(S);
end
A = cat(3,C{:}); % join all of the original matrices into a 107x36x429 array
M = mean(A,3)

but I get the following error message:

Reference to non-existent field 'name'.
Error in myscript (line 6)
S = load(fullfile(D,S(k).name));
1
So which part of the process are you having a problem with? If you say "all of them", I'm afraid your question will be closed as "too broad" and downvoted as "no attempt". Please show us what you've done so far, and which specific step you're struggling with (or would like advice on optimizing, if that's the case). - Dev-iL

1 Answers

0
votes

You are looping over S yet overwriting it every time. Rename the S that is inside the loop. While you are at it I would give all your variables meaningful names--there is no need to use a single letter every time. – svdc

Thank you! I renamed

S = load(fullfile(D,S(k).name)); 

as

T = load(fullfile(D,S(k).name));

and it worked