Home > vbmeg > functions > common > utility > vb_saver_cmn_make_binary_file.m

vb_saver_cmn_make_binary_file

PURPOSE ^

(utility to save data) make binary file of channel data

SYNOPSIS ^

function [data_dir, filenames] = vb_saver_cmn_make_binary_file(proc_spec)

DESCRIPTION ^

 (utility to save data) make binary file of channel data
 [usage]
   [data_dir, filenames] = vb_saver_cmn_make_binary_file(proc_spec)

 [input]
   proc_spec : <required> <<struct>> condition to save
             :  following fields are all required
             :  .data      : channel data  [n_channel x n_sample]
             :  .ch_label  : channel label {1 x n_channel}
             :  .dir       : directory where channel data will be saved
             :  .ext       : extension of channel data file
             :             : e.g. (MEG) ch.meg.dat, (EEG) ch.eeg.dat
             :  .precision : precision to be saved
             :             : e.g. 'float64'

 [output]
    data_dir : data directory
   filenames : file names of channel data to be stored

 [note]
   the number of channel of proc_spec.data and proc_spec.ch_label
   must be the same.

   See also:
   vb_define_device.m

 [history]
   2013-02-07 (Sako) initial version

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [data_dir, filenames] = vb_saver_cmn_make_binary_file(proc_spec)
0002 % (utility to save data) make binary file of channel data
0003 % [usage]
0004 %   [data_dir, filenames] = vb_saver_cmn_make_binary_file(proc_spec)
0005 %
0006 % [input]
0007 %   proc_spec : <required> <<struct>> condition to save
0008 %             :  following fields are all required
0009 %             :  .data      : channel data  [n_channel x n_sample]
0010 %             :  .ch_label  : channel label {1 x n_channel}
0011 %             :  .dir       : directory where channel data will be saved
0012 %             :  .ext       : extension of channel data file
0013 %             :             : e.g. (MEG) ch.meg.dat, (EEG) ch.eeg.dat
0014 %             :  .precision : precision to be saved
0015 %             :             : e.g. 'float64'
0016 %
0017 % [output]
0018 %    data_dir : data directory
0019 %   filenames : file names of channel data to be stored
0020 %
0021 % [note]
0022 %   the number of channel of proc_spec.data and proc_spec.ch_label
0023 %   must be the same.
0024 %
0025 %   See also:
0026 %   vb_define_device.m
0027 %
0028 % [history]
0029 %   2013-02-07 (Sako) initial version
0030 
0031 % --- CHECK ARGUMETNS --- %
0032 if ~exist('proc_spec', 'var'), proc_spec = []; end
0033 [proc_spec] = inner_check_arguments(proc_spec);
0034 
0035 % --- MAIN PROCEDURE --------------------------------------------------------- %
0036 %
0037 n_ch = size(proc_spec.data, 1);
0038 data_dir = proc_spec.dir;
0039 filenames = cell(1, n_ch);
0040 
0041 for i_ch = 1:n_ch
0042   
0043   filename = sprintf('%s%s', proc_spec.ch_label{i_ch}, proc_spec.ext);
0044   new_file = sprintf('%s/%s', data_dir, filename);
0045   fid = fopen(new_file, 'wb');
0046   if fid == -1
0047     warining('(%s) cannot open file : %s\n', mfilename, new_file);
0048     continue;
0049   end
0050   fwrite(fid, proc_spec.data(i_ch, :), proc_spec.precision);
0051   fclose(fid);
0052   
0053   filenames{i_ch} = filename;
0054 end
0055 
0056 return
0057 
0058 end
0059 %
0060 % --- END OF MAIN PROCEDURE -------------------------------------------------- %
0061 
0062 % --- INNER FUNCTIONS -------------------------------------------------------- %
0063 %
0064 % --- inner_check_arguments()
0065 %
0066 function [proc_spec] = inner_check_arguments(proc_spec)
0067 func_ = mfilename;
0068 
0069 % --- proc_spec
0070 if isempty(proc_spec)
0071   error('(%s) proc_spec is a required parameter', func_);
0072 end
0073 
0074 % ----- proc_spec.data
0075 if ~isfield(proc_spec, 'data') || isempty(proc_spec.data)
0076   error('(%s) proc_spec.data is a required field', func_);
0077 end
0078 
0079 % ----- proc_spec.ch_label
0080 if ~isfield(proc_spec, 'ch_label') || isempty(proc_spec.ch_label)
0081   error('(%s) proc_spec.ch_label is a required field', func_);
0082 end
0083 
0084 if ~iscell(proc_spec.ch_label)
0085   proc_spec.ch_label = {proc_spec.ch_label};
0086 end
0087 
0088 n_ch_data = size(proc_spec.data, 1);
0089 n_ch_label = length(proc_spec.ch_label);
0090 
0091 if n_ch_data ~= n_ch_label
0092   error('(%s) the number of channel must be the same data and ch_label', ...
0093     func_);
0094 end
0095 
0096 % ----- proc_spec.dir
0097 if ~isfield(proc_spec, 'dir') || isempty(proc_spec.dir)
0098   error('(%s) proc_spec.dir is a required field', func_);
0099 end
0100 
0101 % ----- proc_spec.precision
0102 if ~isfield(proc_spec, 'precision') || isempty(proc_spec.precision)
0103   error('(%s) proc_spec.precision is a required parameter', func_);
0104 end
0105 
0106 % ----- proc_spec.ext
0107 if ~isfield(proc_spec, 'ext') || isempty(proc_spec.ext)
0108   error('(%s) proc_spec.ext is a required parameter', func_);
0109 end
0110 
0111 if proc_spec.ext(1) ~= '.'
0112   proc_spec.ext = ['.' proc_spec.ext];
0113 end
0114 
0115 if exist(proc_spec.dir, 'dir') ~= 7
0116   fprintf('(%s) make directory : %s\n', func_, proc_spec.dir);
0117   mkdir(proc_spec.dir);
0118 end
0119 return
0120 end
0121 %
0122 % --- end of inner_check_arguments()
0123 %
0124 % --- END OF INNER FUNCTIONS ------------------------------------------------- %
0125 
0126 
0127 % --- END OF FILE --- %

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