Home > atmlab > files > loadvar.m

loadvar

PURPOSE ^

LOADVAR Loads a specific variable from a MATLAB data file

SYNOPSIS ^

function x = loadvar( filename, varname )

DESCRIPTION ^

 LOADVAR   Loads a specific variable from a MATLAB data file

    The function enables direct loading of a named variable (matlab's load
    function puts into a struct). To load the variable x from file data.mat:

        x = loadvar( 'data.mat', 'x' )

 FORMAT   x = loadvar( filename, varname )

 OUT   x          Loaded variable
 IN    filename   Name of file.
       varname    Name of variable.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

loadvar.m

SOURCE CODE ^

0001 % LOADVAR   Loads a specific variable from a MATLAB data file
0002 %
0003 %    The function enables direct loading of a named variable (matlab's load
0004 %    function puts into a struct). To load the variable x from file data.mat:
0005 %
0006 %        x = loadvar( 'data.mat', 'x' )
0007 %
0008 % FORMAT   x = loadvar( filename, varname )
0009 %
0010 % OUT   x          Loaded variable
0011 % IN    filename   Name of file.
0012 %       varname    Name of variable.
0013 
0014 % 2008-10-27   Created by Patrick Eriksson.
0015 
0016 
0017 function x = loadvar( filename, varname )
0018 
0019 try
0020     X = load( filename, varname );
0021 catch ME
0022     switch ME.identifier
0023         case 'MATLAB:load:numColumnsNotSame'
0024             % Matlab tried to load as ASCII and failed.
0025             % Try to load as binary MAT-file instead.
0026             X = load(filename, varname, '-mat');
0027         otherwise
0028             ME.rethrow();
0029     end
0030 end
0031 
0032 if ~isfield( X, varname )
0033     error( ['atmlab:' mfilename ':variableNotFound'],...
0034         lastwarn);
0035 else
0036     x = X.(varname);
0037 end
0038 
0039 end

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005