Home > vbmeg > 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
   2017-11-13 (rhayashi) create directory when save directory not exist

 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 %   2017-11-13 (rhayashi) create directory when save directory not exist
0015 %
0016 % Copyright (C) 2011, ATR All Rights Reserved.
0017 % License : New BSD License(see VBMEG_LICENSE.txt)
0018 
0019 % --- CHECK ARGUMENTS --- %
0020 if ~exist('filename', 'var'), filename = ''; end
0021 if ~exist('struct_obj', 'var'), struct_obj = []; end
0022 [filename, struct_obj] = inner_check_arguments(filename, struct_obj);
0023 
0024 % --- MAIN PROCEDURE --------------------------------------------------------- %
0025 %
0026 global vbmeg_saving_version
0027 
0028 % [PATH, NAME, EXT] = fileparts(file_name);
0029 [PATH, NAME, EXT] = vb_get_file_parts(filename);
0030 if ~isempty(PATH) && ~exist(PATH, 'dir')
0031     vb_mkdir(PATH);
0032     vb_disp(['Create directory: ' PATH]);
0033 end
0034 
0035 EXT = '.mat';
0036 filename = fullfile(PATH, [NAME, EXT]);
0037 
0038 compatible_option = vbmeg_saving_version;
0039 
0040 if vb_matlab_version('>=', '7')
0041   save_cmd = sprintf('save %s -struct struct_obj %s', filename, compatible_option);
0042 else
0043   fields = fieldnames(struct_obj);
0044   n_field = length(fields);
0045   save_cmd = sprintf('vb_fsave(''%s''', filename);
0046 
0047   for i_field = 1:n_field
0048     cur_field = fields{i_field};
0049 
0050     save_cmd = sprintf('%s, ''%s''', save_cmd, cur_field);
0051 
0052     setvar_cmd = sprintf('%s = %s.%s;', cur_field, 'struct_obj', cur_field);
0053     eval(setvar_cmd);
0054   end
0055   save_cmd = [save_cmd ');'];
0056 end
0057 
0058 %fprintf('EXEC: %s\n', save_cmd);
0059 eval(save_cmd);
0060 return;
0061 %
0062 % --- END OF MAIN PROCEDURE -------------------------------------------------- %
0063 
0064 % --- INNER FUNCTIONS -------------------------------------------------------- %
0065 %
0066 % --- inner_check_arguments()
0067 %
0068 function [filename, struct_obj, result] = ...
0069   inner_check_arguments(filename, struct_obj)
0070 func_ = mfilename;
0071 
0072 if isempty(filename)
0073   error('(%s) filename is a required parameter', func_);
0074 end
0075 
0076 if isempty(struct_obj)
0077   error('(%s) struct_obj is a required parameter', func_);
0078 end
0079 
0080 [PATH, NAME, EXT] = vb_get_file_parts(filename);
0081 if ~isempty(PATH) && ~exist(PATH, 'dir')
0082     vb_mkdir(PATH);
0083     vb_disp(['Create directory: ' PATH]);
0084 end
0085 EXT = '.mat';
0086 filename = fullfile(PATH, [NAME, EXT]);
0087 
0088 return;
0089 %
0090 % --- end of inner_check_arguments()
0091 %
0092 % --- END OF INNER FUNCTIONS ------------------------------------------------- %
0093 
0094 %%% END OF FILE %%%

Generated on Mon 22-May-2023 06:53:56 by m2html © 2005