Home > utils > mkstruct.m

mkstruct

PURPOSE ^

Returns a structure using the provided fieldnames and values

SYNOPSIS ^

function st = mkstruct(fields,args)

DESCRIPTION ^

 Returns a structure using the provided fieldnames and values

 st = mkstruct(fields,args);



 INPUTS
 fields - cell array of fieldnames to populate. 
 args - a list of param/value pairs containing the name of the field and the
        value it should be populated with

 Copyright (c) 2006-2012 The Regents of the University of California
 All Rights Reserved.

 Author:
 12/04/06 Petr Janata

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function st = mkstruct(fields,args)
0002 % Returns a structure using the provided fieldnames and values
0003 %
0004 % st = mkstruct(fields,args);
0005 %
0006 %
0007 %
0008 % INPUTS
0009 % fields - cell array of fieldnames to populate.
0010 % args - a list of param/value pairs containing the name of the field and the
0011 %        value it should be populated with
0012 %
0013 % Copyright (c) 2006-2012 The Regents of the University of California
0014 % All Rights Reserved.
0015 %
0016 % Author:
0017 % 12/04/06 Petr Janata
0018 
0019 st = [];
0020 
0021 if nargin < 2
0022   args = {};
0023 end
0024 
0025 nflds = length(fields);
0026 % Initialize a structure with the fields
0027 for ifld = 1:nflds
0028   st.(fields{ifld}) = [];
0029 end
0030 
0031 % Handle the input arguments
0032 nargs = length(args);
0033 if nargs
0034   for iarg = 1:2:nargs
0035     tag = args{iarg};
0036     if ~isstr(tag)
0037       errorstr = sprintf('Parameter tag must be a string');
0038       error(errorstr);
0039     end
0040     
0041     % Make sure the tag exists
0042     if ~any(ismember(lower(fields),lower(tag)))
0043       fprintf('%s: Skipping unknown tag: %s\n', mfilename, tag);
0044       continue
0045     end
0046 
0047     % Assign the field the value
0048     st.(tag) = args{iarg+1};
0049   end % for iarg
0050 end % if nargin

Generated on Wed 20-Sep-2023 04:00:50 by m2html © 2003