%% Load Parts of Variables Dynamically
% This example shows how to access parts of variables from a MAT-file dynamically. This is useful when working with MAT-files whose variables names are not always known.
% Consider the example MAT-file, topography.mat, that contains one or more arrays with unknown names. Construct a MatFile object that corresponds to the file, topography.mat. Call who to get the variable names in the file.

exampleObject = matfile('topography.mat');
varlist = who(exampleObject)

% varlist is a cell array containing the names of the four variables in topography.mat.
%
% The third and fourth variables, topomap1 and topomap2, are both arrays containing topography data. Load the elevation data from the third column of each variable into a field of the structure array, S. For each field, specify a field name that is the original variable name prefixed by elevationOf_. Then, access the data in each variable as properties of exampleObject. Because varName is a variable, enclose it in parentheses.

for index = 3:4
    varName = varlist{index};
    S(1).(['elevationOf_',varName]) = exampleObject.(varName)(:,3);
end

%
% View the contents of the structure array, S.

S

% S has two fields, elevationOf_topomap1 and elevationOf_topomap2, each containing a column vector.
% Copyright 2012 The MathWorks, Inc.