Home > matpower6.0 > most > loadgenericdata.m

loadgenericdata

PURPOSE ^

LOADGENERICDATA Loads data of a specified type from file or data structure.

SYNOPSIS ^

function var = loadgenericdata(varfile, vartype, fields, varname, args)

DESCRIPTION ^

LOADGENERICDATA Loads data of a specified type from file or data structure.

   VAR = LOADGENERICDATA(VARFILE, VARTYPE)
   VAR = LOADGENERICDATA(VARFILE, VARTYPE, FIELDS)
   VAR = LOADGENERICDATA(VARFILE, VARTYPE, FIELDS, VARNAME)
   VAR = LOADGENERICDATA(VARFILE, VARTYPE, FIELDS, VARNAME, ARGS)

   Loads data from a variable or M-file or MAT-file and checks that it
   matches a specified type.

   Inputs:
       VARFILE : Variable containing the data structure or a string
                 containing the name of a function M-file or a MAT-file
                 on the Matlab path. If no file extension is provided
                 it will attempt to load a MAT-file with that name and,
                 if not found, will call a function by that name to
                 get the data. The function M-file should return a
                 single argument containing the data. A MAT-file should
                 either contain a single variable with the desired data
                 or provide the variable name in VARNAME.
       VARTYPE : String or cell array of strings with, in order of
                 priority, the data structure type to be returned.
                 Valid values are 'struct', 'cell' and 'array'.
       FIELDS :  (optional) String or cell array of strings containing
                 a list of required fields in case the VARTYPE is struct.
                 If a required field is missing it will throw an error.
       VARNAME   (optional) String containing the name of the variable to
                 extract when loading a MAT-file. If not provided, the
                 default is to extract the 1st variable, regardless of name.
       ARGS :    (optional) Scalar or cell array of values that are passed
                 as input function arguments to VARFILE if it is an M-file.

   Output:
       VAR : Returned data structure of the first matching VARTYPE.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function var = loadgenericdata(varfile, vartype, fields, varname, args)
0002 %LOADGENERICDATA Loads data of a specified type from file or data structure.
0003 %
0004 %   VAR = LOADGENERICDATA(VARFILE, VARTYPE)
0005 %   VAR = LOADGENERICDATA(VARFILE, VARTYPE, FIELDS)
0006 %   VAR = LOADGENERICDATA(VARFILE, VARTYPE, FIELDS, VARNAME)
0007 %   VAR = LOADGENERICDATA(VARFILE, VARTYPE, FIELDS, VARNAME, ARGS)
0008 %
0009 %   Loads data from a variable or M-file or MAT-file and checks that it
0010 %   matches a specified type.
0011 %
0012 %   Inputs:
0013 %       VARFILE : Variable containing the data structure or a string
0014 %                 containing the name of a function M-file or a MAT-file
0015 %                 on the Matlab path. If no file extension is provided
0016 %                 it will attempt to load a MAT-file with that name and,
0017 %                 if not found, will call a function by that name to
0018 %                 get the data. The function M-file should return a
0019 %                 single argument containing the data. A MAT-file should
0020 %                 either contain a single variable with the desired data
0021 %                 or provide the variable name in VARNAME.
0022 %       VARTYPE : String or cell array of strings with, in order of
0023 %                 priority, the data structure type to be returned.
0024 %                 Valid values are 'struct', 'cell' and 'array'.
0025 %       FIELDS :  (optional) String or cell array of strings containing
0026 %                 a list of required fields in case the VARTYPE is struct.
0027 %                 If a required field is missing it will throw an error.
0028 %       VARNAME   (optional) String containing the name of the variable to
0029 %                 extract when loading a MAT-file. If not provided, the
0030 %                 default is to extract the 1st variable, regardless of name.
0031 %       ARGS :    (optional) Scalar or cell array of values that are passed
0032 %                 as input function arguments to VARFILE if it is an M-file.
0033 %
0034 %   Output:
0035 %       VAR : Returned data structure of the first matching VARTYPE.
0036 
0037 %   MOST
0038 %   Copyright (c) 2013-2016, Power Systems Engineering Research Center (PSERC)
0039 %   by Daniel Munoz-Alvarez and Ray Zimmerman, PSERC Cornell
0040 %
0041 %   This file is part of MOST.
0042 %   Covered by the 3-clause BSD License (see LICENSE file for details).
0043 %   See http://www.pserc.cornell.edu/matpower/ for more info.
0044 
0045 if isempty(varfile)
0046     var = [];
0047     return;
0048 end
0049 if nargin < 5
0050     args = [];
0051     if nargin < 4
0052         varname = '';
0053         if nargin < 3
0054             fields = {};
0055         end
0056     end
0057 end
0058 
0059 if ischar(varfile)      %% it's a file name
0060     %% check for explicit extension
0061     l = length(varfile);
0062     if l > 2
0063         if strcmp(varfile(l-1:l), '.m')
0064             rootname = varfile(1:l-2);
0065             extension = '.m';
0066         elseif l > 4
0067             if strcmp(varfile(l-3:l), '.mat')
0068                 rootname = varfile(1:l-4);
0069                 extension = '.mat';
0070             end
0071         end
0072     end
0073 
0074     %% set extension if not specified explicitly
0075     if ~exist('rootname', 'var')
0076         rootname = varfile;
0077         if exist([rootname '.mat'], 'file') == 2
0078             extension = '.mat';
0079         elseif exist([rootname '.m'], 'file') == 2
0080             extension = '.m';
0081         else
0082             error('loadgenericdata: No file named ''%s.mat'' or ''%s.m'' found in Matlab path.', rootname, rootname);
0083         end
0084     end
0085 
0086     %% attempt to read file
0087     if strcmp(extension,'.mat')         %% from MAT file
0088         s = load(rootname);
0089         if isempty(varname)
0090             f = fieldnames(s);
0091             var = s.(f{1});             %% use first variable if not specified
0092         elseif isfield(s, varname)
0093             var = s.(varname);          %% use specified variable
0094         else
0095             error('loadgenericdata: No variable named ''%s'' in ''%s.mat''', varname, rootname);
0096         end
0097     elseif strcmp(extension,'.m')       %% from M file
0098         if isempty(args)
0099             var = feval(rootname);
0100         elseif iscell(args)
0101             var = feval(rootname, args{:});
0102         else
0103             var = feval(rootname, args);
0104         end
0105     end
0106 else                    %% it's a data structure, not a file name
0107     var = varfile;
0108 end
0109 
0110 %% check the provided types
0111 if ~iscell(vartype)
0112     vartype = {vartype};    %% convert string to single element cell array
0113 end
0114 if ~iscell(fields) && ~isempty(fields)
0115     fields = {fields};      %% convert string to single element cell array
0116 end
0117 done = false;
0118 for i = length(vartype)
0119     switch vartype{i}
0120         case 'struct'
0121             if isstruct(var)
0122                 for j = 1:length(fields)
0123                     if ~ischar(fields{j})
0124                         error('loadgenericdata: FIELDS input must be a string or cell array of strings.')
0125                     end
0126                     if ~isfield(var, fields{j})
0127                         error('loadgenericdata:missingfield', 'loadgenericdata: field ''%s'' missing from struct data.', fields{j});
0128                     end
0129                 end
0130                 done = true; break;     %% struct OK
0131             end
0132 
0133         case 'cell'
0134             if iscell(var)
0135                 done = true; break;     %% cell array OK
0136             end
0137 
0138         case 'array'
0139             if isnumeric(var)
0140                 done = true; break;     %% array OK
0141             end
0142         otherwise
0143             error('loadgenericdata: ''%s'' is not a valid value for VARTYPE', vartype{i});
0144     end
0145 end
0146 if ~done
0147     error('loadgenericdata: data not found in any of the specified formats');
0148 end

Generated on Fri 16-Dec-2016 12:45:37 by m2html © 2005