www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/LoadfromVariableswithUnknownNamesExample.m

    %% Load from Variables with Unknown Names   
% This example shows how to dynamically access variables, whose names are
% not always known. Consider the example MAT-file, |topography.mat|, that
% contains one or more arrays with unknown names.   

%% 
% Construct a |matlab.io.MatFile| object that corresponds to the file, |topography.mat|.
% Call |who| to get the variable names in the file. 
matObj = matfile('topography.mat');
varlist = who(matObj) 

%%
% |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_|.
% Access the data in each variable as properties of |matObj|. Because |varName|
% is a variable, enclose it in parentheses. 
for index = 3:4
    varName = varlist{index};
    S(1).(['elevationOf_',varName]) = matObj.(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.