首页 文章

将多个.mat文件加载到矩阵中并对其执行操作

提问于
浏览
1

我有429个相同大小的数字矩阵(107行乘36列),存储在顺序命名为 .mat 的文件中(例如: subj_00000.mat ... subj_00428.mat ) . 这是我需要做的:

  • 导入MATLAB工作区 .

  • 导入后,平均所有矩阵以生成另一个矩阵,其尺寸也为107x36 .

  • 最后,将平均矩阵的每列与每个原始429矩阵的每列线性相关,以生成429行和36列的新矩阵 .

到目前为止,我已经到了构建一个107 x 36 x 429阵列的阶段,以填充我的矩阵集 .

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)

但是我收到以下错误消息:

Reference to non-existent field 'name'.
Error in myscript (line 6)
S = load(fullfile(D,S(k).name));

1 回答

  • 0

    你正在循环S,但每次都覆盖它 . 重命名循环内的S.当你在它的时候,我会给你所有的变量都有意义的名字 - 每次都不需要使用一个字母 . - svdc

    谢谢!我重命名了

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

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

    它工作

相关问题