Home > vbmeg > functions > gui > batch > batch_mgr > vb_struct2file.m

vb_struct2file

PURPOSE ^

Write structure information evaluated by command window.

SYNOPSIS ^

function file = vb_struct2file(p, file)

DESCRIPTION ^

 Write structure information evaluated by command window.

 [USAGE]
    file = vb_struct2file(p [,file]);
 [IN]
       p : struct to be evaluated
    file : filename[optional]
           if this is empty, The file is created by tempname function.
 [OUT]
    file : created filename
 [HISTORY]
    2010-11-17 rhayashi
 [REFERENCE]
    evalc

 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 file = vb_struct2file(p, file)
0002 % Write structure information evaluated by command window.
0003 %
0004 % [USAGE]
0005 %    file = vb_struct2file(p [,file]);
0006 % [IN]
0007 %       p : struct to be evaluated
0008 %    file : filename[optional]
0009 %           if this is empty, The file is created by tempname function.
0010 % [OUT]
0011 %    file : created filename
0012 % [HISTORY]
0013 %    2010-11-17 rhayashi
0014 % [REFERENCE]
0015 %    evalc
0016 %
0017 % Copyright (C) 2011, ATR All Rights Reserved.
0018 % License : New BSD License(see VBMEG_LICENSE.txt)
0019 
0020 %
0021 % --- Previous check
0022 %
0023 if ~isstruct(p)
0024     error('input variable is not struct.');
0025 end
0026 
0027 %
0028 % --- receive comand window output and write file
0029 %
0030 if ~exist('file', 'var')
0031     file = tempname;
0032 end
0033 fid = fopen(file, 'wt');
0034 if fid == -1
0035     error('failed to open file(w)');
0036 end
0037 a = evalc('p');
0038 fprintf(fid, '%s', a);
0039 fclose(fid);
0040 
0041 %
0042 % --- Formatting
0043 %
0044 fid = fopen(file);
0045 if fid == -1
0046     error('failed to open file(r).');
0047 end
0048 
0049 all = [];
0050 while 1
0051     line = fgetl(fid);
0052     if line == -1, break; end
0053     % remove top line
0054     if strmatch('p =', line), continue; end
0055     % remove empty line
0056     if isempty(line), continue; end
0057 
0058     % forced to expand the character string
0059     field = sscanf(line, ' %s:');
0060     field = strrep(field, ':', '');
0061 
0062     para = p.(field);
0063     if ischar(para)
0064         ix = strfind(line, ':');
0065         line = [line(1:ix(1)) ' ' single_quoted_string(para)];
0066     end
0067     all = [all, sprintf('%s\n', line)];
0068 end
0069 fclose(fid);
0070 
0071 
0072 
0073 %
0074 % --- Write the formatted string into file
0075 %
0076 fid = fopen(file, 'wt');
0077 if fid == -1
0078     error('failed to open file(w)');
0079 end
0080 fprintf(fid, '%s', all);
0081 fclose(fid);
0082 
0083 
0084 
0085 function out_str = single_quoted_string(in_str)
0086 
0087     out_str = sprintf('''%s''', in_str);

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