loadStruct

PURPOSE ^

LOADSTRUCT load each field of structure and save them to specified workspace.

SYNOPSIS ^

function [ status ] = loadStruct( structure, workspaceName )

DESCRIPTION ^

 LOADSTRUCT load each field of structure and save them to specified workspace.

   [ status ] = loadStruct( structure )
   This function load each field of structure and save them to specified
   workspace with each field name as variable name. Default workspaceName
   is 'base'.

   Input:
       structure: a struct variable
       workspaceName: 'base' workspace or 'caller' workspace

   Output:
       status: represent success or not (true -> success, [] -> fail)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [ status ] = loadStruct( structure, workspaceName )
0002 % LOADSTRUCT load each field of structure and save them to specified workspace.
0003 %
0004 %   [ status ] = loadStruct( structure )
0005 %   This function load each field of structure and save them to specified
0006 %   workspace with each field name as variable name. Default workspaceName
0007 %   is 'base'.
0008 %
0009 %   Input:
0010 %       structure: a struct variable
0011 %       workspaceName: 'base' workspace or 'caller' workspace
0012 %
0013 %   Output:
0014 %       status: represent success or not (true -> success, [] -> fail)
0015 
0016 %   ---------
0017 %   Yen-Nan Lin, NTHU, 2010-2014, Matlab 2012a
0018 
0019 if nargin < 2
0020     workspaceName = 'base';
0021 end
0022 
0023 fieldName = fieldnames(structure);
0024 for iName = 1:numel(fieldName)
0025     variableName = fieldName{iName};
0026     % check this variable name exist in specified workspace or not
0027     isExistName = evalin(workspaceName, ...
0028         ['exist(''', variableName, ''', ''var'');']);
0029     if isExistName
0030         error(['Already exist a variable named as ''', variableName, '''.']);
0031     end
0032     variableContent = getfield(structure, variableName);
0033     assignin(workspaceName, variableName, variableContent);
0034 end
0035 status = true;
0036 end

Generated on Thu 30-Jan-2014 00:00:07 by m2html © 2005