0001 function vb_save_struct(filename, struct_obj)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
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
0025
0026 global vbmeg_saving_version
0027
0028
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
0059 eval(save_cmd);
0060 return;
0061
0062
0063
0064
0065
0066
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
0091
0092
0093
0094