Search code examples
matlabfunctionloadmat-file

MATLAB: How to access a matrix loaded from a .mat file from a function?


I have three .mat files, m1.mat, m2.mat and m3.mat, each representing a cell array m1, m2 and m3 respectively. I need to access the cell arrays programatically from within a function. Inside the function I declare an array of strings to store the filenames. I loop through the filenames and within each loop load the file and try to find dimensions of the cell array in the file.

The question is: how do I access the cell array? If I say x = load ('m1.mat') it does not return a matrix, it just says

m1: {10x2 cell}
size(m1) % return 1 1

Any help will be greatly appreciated.


Solution

  • try using dynamic feild names

    for nm = {'m1', 'm2', 'm3' }
        ld = load( [nm{1},'.mat'] );
        ca = ld.(nm{1}); % ca should hold the cell array loaded from the file
        size( ca )
    end