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

vb_util_check_variable_in_matfile

PURPOSE ^

check states of a variable of mat file

SYNOPSIS ^

function [state, guide_def] = vb_util_check_variable_in_matfile(mat_file, hot_var)

DESCRIPTION ^

 check states of a variable of mat file
 [usage]
   [state, guide_def] = vb_util_check_variable_in_matfile(mat_file, hot_var)
 [input]
   mat_file : <required> <<file>> .mat file to be checked
    hot_var : <required> name of variable which you want to check
 [output]
      state : <<integer>> state code defind by bit sequence as follows:
            :  1) (01) it exists
            :  2) (10) it is not empty
  guide_def : meaning of state
            : (3) VALID ------- hot_var exist and is not empty
            : (1) VACANT ------ hot_var exist but is empty
            : (0) NONEXISTENT - hot_var does not exist in mat_file
 [note]
   <example>
     hot_var = 'a_var';
     [state, const] = vb_util_check_variable_in_matfile(sample_mat, hot_var);
     if state == const.VALID
       fprintf('VALID\n');
     elseif state == const.VACANT
       fprintf('VACANT\n');
     elseif state == const.NONEXISTENT
       fprintf('NONEXISTENT\n');
     else
       fprintf('SOMETHING BAD STATE (%d)\n', state);
     end
   
   @see vb_meg_tutorial(20001)
   @see whos
 [history]
   2010-02-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 [state, guide_def] = vb_util_check_variable_in_matfile(mat_file, hot_var)
0002 % check states of a variable of mat file
0003 % [usage]
0004 %   [state, guide_def] = vb_util_check_variable_in_matfile(mat_file, hot_var)
0005 % [input]
0006 %   mat_file : <required> <<file>> .mat file to be checked
0007 %    hot_var : <required> name of variable which you want to check
0008 % [output]
0009 %      state : <<integer>> state code defind by bit sequence as follows:
0010 %            :  1) (01) it exists
0011 %            :  2) (10) it is not empty
0012 %  guide_def : meaning of state
0013 %            : (3) VALID ------- hot_var exist and is not empty
0014 %            : (1) VACANT ------ hot_var exist but is empty
0015 %            : (0) NONEXISTENT - hot_var does not exist in mat_file
0016 % [note]
0017 %   <example>
0018 %     hot_var = 'a_var';
0019 %     [state, const] = vb_util_check_variable_in_matfile(sample_mat, hot_var);
0020 %     if state == const.VALID
0021 %       fprintf('VALID\n');
0022 %     elseif state == const.VACANT
0023 %       fprintf('VACANT\n');
0024 %     elseif state == const.NONEXISTENT
0025 %       fprintf('NONEXISTENT\n');
0026 %     else
0027 %       fprintf('SOMETHING BAD STATE (%d)\n', state);
0028 %     end
0029 %
0030 %   @see vb_meg_tutorial(20001)
0031 %   @see whos
0032 % [history]
0033 %   2010-02-10 (Sako) initial version
0034 %
0035 % Copyright (C) 2011, ATR All Rights Reserved.
0036 % License : New BSD License(see VBMEG_LICENSE.txt)
0037 
0038 % --- CHECK ARGUMENTS --- %
0039 if ~exist('mat_file', 'var'), mat_file = ''; end
0040 if ~exist('hot_var', 'var'), hot_var = ''; end
0041 [mat_file, hot_var] = inner_check_arguments(mat_file, hot_var);
0042 
0043 % --- MAIN PROCEDURE --------------------------------------------------------- %
0044 %
0045 % --- constants
0046 NONEXISTENT = 0;
0047    EXISTENT = 1;
0048   NOT_EMPTY = 2;
0049 
0050 guide_def.NONEXISTENT = 0;
0051 guide_def.VACANT = 1;
0052 guide_def.VALID  = 3;
0053 
0054 state = NONEXISTENT;
0055 
0056 mat_head = whos('-file', mat_file);
0057 n_head = size(mat_head,1);
0058 for i_head = 1:n_head
0059   if strcmp(mat_head(i_head).name, hot_var)
0060     state = bitor(state, EXISTENT);
0061     if mat_head(i_head).bytes > 0
0062       state = bitor(state, NOT_EMPTY);
0063     end
0064     break;
0065   end
0066 end
0067 return;
0068 %
0069 % --- END OF MAIN PROCEDURE -------------------------------------------------- %
0070 
0071 % --- INNER FUNCTIONS -------------------------------------------------------- %
0072 %
0073 % --- inner_check_arguments()
0074 %
0075 function [mat_file, hot_var] = inner_check_arguments(mat_file, hot_var)
0076 func_ = mfilename;
0077 if isempty(mat_file)
0078   error('(%s) mat_file is a required parameter', func_);
0079 end
0080 
0081 if exist(mat_file, 'file') ~= 2
0082   error('(%s) cannot find %s', func_, mat_file);
0083 end
0084 
0085 if isempty(hot_var)
0086   error('(%s) hot_var is a required parameter', func_);
0087 end
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