Home > functions > common > utility > vb_save_struct.m

vb_save_struct

PURPOSE ^

save the fields of "struct_obj" as individual variables

SYNOPSIS ^

function vb_save_struct(filename, struct_obj)

DESCRIPTION ^

 save the fields of "struct_obj" as individual variables
 [usage]
   vb_save_struct(filename, struct_obj)
 [input]
     filename : <required> <<file>> save the fields of "struct_obj" to this
   struct_obj : <required> <<struct>> struct the fields of which are saved
 [output]
   none
 [note]
   @see vb_matlab_version.m
 [history]
   2009-08-10 (Sako) initial version

 Copyright (C) 2011, ATR All Rights Reserved.
 License : New BSD License(see VBMEG_LICENSE.txt)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function vb_save_struct(filename, struct_obj)
0002 % save the fields of "struct_obj" as individual variables
0003 % [usage]
0004 %   vb_save_struct(filename, struct_obj)
0005 % [input]
0006 %     filename : <required> <<file>> save the fields of "struct_obj" to this
0007 %   struct_obj : <required> <<struct>> struct the fields of which are saved
0008 % [output]
0009 %   none
0010 % [note]
0011 %   @see vb_matlab_version.m
0012 % [history]
0013 %   2009-08-10 (Sako) initial version
0014 %
0015 % Copyright (C) 2011, ATR All Rights Reserved.
0016 % License : New BSD License(see VBMEG_LICENSE.txt)
0017 
0018 % --- CHECK ARGUMENTS --- %
0019 if ~exist('filename', 'var'), filename = ''; end
0020 if ~exist('struct_obj', 'var'), struct_obj = []; end
0021 [filename, struct_obj] = inner_check_arguments(filename, struct_obj);
0022 
0023 % --- MAIN PROCEDURE --------------------------------------------------------- %
0024 %
0025 if vb_matlab_version('>=', '7')
0026   save_cmd = sprintf('save %s -struct struct_obj', filename);
0027 else
0028   fields = fieldnames(struct_obj);
0029   n_field = length(fields);
0030   save_cmd = sprintf('vb_fsave(''%s''', filename);
0031 
0032   for i_field = 1:n_field
0033     cur_field = fields{i_field};
0034 
0035     save_cmd = sprintf('%s, ''%s''', save_cmd, cur_field);
0036 
0037     setvar_cmd = sprintf('%s = %s.%s;', cur_field, 'struct_obj', cur_field);
0038     eval(setvar_cmd);
0039   end
0040   save_cmd = [save_cmd ');'];
0041 end
0042 
0043 fprintf('EXEC: %s\n', save_cmd);
0044 eval(save_cmd);
0045 return;
0046 %
0047 % --- END OF MAIN PROCEDURE -------------------------------------------------- %
0048 
0049 % --- INNER FUNCTIONS -------------------------------------------------------- %
0050 %
0051 % --- inner_check_arguments()
0052 %
0053 function [filename, struct_obj, result] = ...
0054   inner_check_arguments(filename, struct_obj)
0055 func_ = mfilename;
0056 
0057 if isempty(filename)
0058   error('(%s) filename is a required parameter', func_);
0059 end
0060 
0061 if isempty(struct_obj)
0062   error('(%s) struct_obj is a required parameter', func_);
0063 end
0064 return;
0065 %
0066 % --- end of inner_check_arguments()
0067 %
0068 % --- END OF INNER FUNCTIONS ------------------------------------------------- %
0069 
0070 %%% END OF FILE %%%

Generated on Tue 27-Aug-2013 11:46:04 by m2html © 2005