Home > functions > gui > eeg_gain_input_dir > gain_input.m

gain_input

PURPOSE ^

This gui is for input eeg gain values.

SYNOPSIS ^

function [varargout] = gain_input(varargin)

DESCRIPTION ^

 This gui is for input eeg gain values.
 When the figure close, eeg gain values are returned.

 Usage : 
 [eeg_gain, cancelled] = gain_input('init', <channel_list> [,gain_list]);

 [IN]
            key  : 'init', 'cancel', 'callback'
    channel_list : Nx1 channel numbers.
    gain_list    : Nx1 gain initial values.(This is optional input)

 [OUT]
    gain : inputted gain value(Nx2)
            gain{:, 1} = channel list{Nx1}
            gain{:, 2} = gain list   {Nx1}
    cancelled : true  % gui closed by pushing cancel or x button.
                false % gui close by pushing ok button.
 Note: inner expression of gain value, 
       gain value = 0 means not inputted gain.


 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 [varargout] = gain_input(varargin)
0002 % This gui is for input eeg gain values.
0003 % When the figure close, eeg gain values are returned.
0004 %
0005 % Usage :
0006 % [eeg_gain, cancelled] = gain_input('init', <channel_list> [,gain_list]);
0007 %
0008 % [IN]
0009 %            key  : 'init', 'cancel', 'callback'
0010 %    channel_list : Nx1 channel numbers.
0011 %    gain_list    : Nx1 gain initial values.(This is optional input)
0012 %
0013 % [OUT]
0014 %    gain : inputted gain value(Nx2)
0015 %            gain{:, 1} = channel list{Nx1}
0016 %            gain{:, 2} = gain list   {Nx1}
0017 %    cancelled : true  % gui closed by pushing cancel or x button.
0018 %                false % gui close by pushing ok button.
0019 % Note: inner expression of gain value,
0020 %       gain value = 0 means not inputted gain.
0021 %
0022 %
0023 % Copyright (C) 2011, ATR All Rights Reserved.
0024 % License : New BSD License(see VBMEG_LICENSE.txt)
0025 if nargin < 1
0026     key = 'init';
0027 else
0028     key = varargin{1};
0029 end
0030 
0031 switch key
0032     case 'init'
0033         gain = [];
0034         if nargin == 2
0035             channel_list = varargin{2};
0036             gain_list = zeros(length(channel_list), 1);
0037         elseif nargin == 3
0038             channel_list = varargin{2};
0039             gain_list    = varargin{3};
0040             
0041             if length(channel_list) ~= length(gain_list)
0042                 error('Number of channels and gain values mismatched.');
0043             end
0044             cl_size = size(channel_list);
0045             gl_size = size(gain_list);
0046             if cl_size(2) > 1
0047                 warning('channel_list size should be Nx1.');
0048                 channel_list = channel_list';
0049             end
0050             if gl_size(2) > 1
0051                 warning('gain_list size should be Nx1.');
0052                 gain_list = gain_list';
0053             end
0054         else
0055             error(USAGE_STRING);
0056         end
0057 
0058         % gain setting
0059         Nchannel = length(channel_list);
0060         gain = cell(Nchannel, 2);
0061         gain(:, 1) = channel_list;
0062         for k=1:Nchannel
0063             gain{k, 2} = gain_list(k);
0064         end
0065 
0066         h = create_figure(gain);
0067         % Wait for application end
0068         waitfor(h, 'Name', 'close');
0069         if ~ishandle(h)
0070             % close by cancel or x button
0071             varargout{1} = [];
0072             varargout{2} = true;
0073             return;
0074         end
0075 
0076         if get_application_status(h) == VALUE_RETURN_EXIT
0077             [gain_data] = get_application_data(h);
0078             % Remove not inputted(=0) channel gain.
0079             null_ix = find([gain_data{:, 2}]  == 0);
0080             gain_data(null_ix, :) = [];
0081             varargout{1} = gain_data;
0082             varargout{2} = false;
0083         elseif get_application_status(h) == VALUE_NO_RETURN_EXIT
0084             varargout{1} = [];
0085             varargout{2} = true;
0086         else
0087             error('Assertion.', 'error');
0088         end
0089         % delete figure
0090         if ishandle(h)
0091             delete(h);
0092         end
0093     case 'cancel'
0094         set_application_status(gcf, VALUE_NO_RETURN_EXIT);
0095         closereq;
0096     case 'callback'
0097         callback(gcf, varargin{2});
0098 end
0099 
0100 function h = create_figure(gain)
0101 % This function create main figure.
0102 % [IN]
0103 %    gain : gain(:, 1) % Nx1 channel list
0104 %           gain(:, 2) % Nx1 gain list
0105 % [OUT]
0106 %    h : created figure handle
0107 
0108     h = openfig(mfilename);
0109     create_item(h, gain);
0110     set_application_data(h, gain);
0111     set_application_status(h, VALUE_NO_RETURN_EXIT);
0112 
0113 function create_item(fig, gain)
0114 % This function creates editboxes and set initial gain value.
0115 %
0116 % [IN]
0117 %     fig : figure handle
0118 %    gain : gain(:, 1) % Nx1 channel list
0119 %           gain(:, 2) % Nx1 gain list
0120 %
0121 
0122     H = guihandles(fig);
0123 
0124     channels  = gain(:, 1);
0125     gain_list = gain(:, 2);
0126 
0127     HORIZONTAL_ITEM_NUM = ceil(length(channels) / VERTICAL_ITEM_NUM);
0128 
0129     % Figure size setting
0130     if HORIZONTAL_ITEM_NUM >= 4
0131         size_f = get(fig, 'Position');
0132         size_f(3) = size_f(3) * 2;
0133         set(fig, 'Position', size_f);
0134     end
0135     
0136     % Unit setting
0137     set(H.unit_text, 'String', UNIT_STRING);
0138         
0139     % Frame setting
0140     FRAME_LEFT_MARGIN = 0.05;
0141     FRAME_RIGHT_MARGIN = 0.05;
0142     FRAME_TOP_MARGIN  = 0.1;
0143     FRAME_BOTTOM_MARGIN = 0.1;
0144 
0145     set(H.frame, 'Position', ...
0146         [FRAME_LEFT_MARGIN, FRAME_BOTTOM_MARGIN, ...
0147         1.0-(FRAME_LEFT_MARGIN + FRAME_RIGHT_MARGIN), ...
0148         1.0-(FRAME_TOP_MARGIN + FRAME_BOTTOM_MARGIN)]);
0149 
0150 
0151     % Item area(=Frame inner) setting
0152     FRAME_INNER_LEFT_MARGIN = 0.02;
0153     FRAME_INNER_RIGHT_MARGIN = 0.02;
0154     FRAME_INNER_TOP_MARGIN   = 0.05;
0155     FRAME_INNER_BOTTOM_MARGIN = 0.05;
0156 
0157     FRAME_INNER_WIDTH  = 1.0 - ...
0158                         (FRAME_LEFT_MARGIN+FRAME_RIGHT_MARGIN+ ...
0159                          FRAME_INNER_LEFT_MARGIN+FRAME_INNER_RIGHT_MARGIN);
0160     FRAME_INNER_HEIGHT = 1.0 - ...
0161                         (FRAME_TOP_MARGIN+FRAME_BOTTOM_MARGIN+ ...
0162                          FRAME_INNER_TOP_MARGIN+FRAME_INNER_BOTTOM_MARGIN);
0163 
0164     % Item Setting
0165     ITEM_WIDTH  = FRAME_INNER_WIDTH / HORIZONTAL_ITEM_NUM;
0166     ITEM_HEIGHT = FRAME_INNER_HEIGHT / VERTICAL_ITEM_NUM;
0167 
0168     CH_TEXT_WIDTH        = ITEM_WIDTH * 0.3; 
0169     GAIN_EDIT_WIDTH      = ITEM_WIDTH * 0.3;
0170     GAIN_UNIT_TEXT_WIDTH = ITEM_WIDTH * 0.4;
0171     
0172     MAX_CH_TEXT_WIDTH = 0.1;
0173     MAX_GAIN_EDIT_WIDTH = 0.08;
0174     MAX_GAIN_UNIT_TEXT_WIDTH = 0.10;
0175     
0176     if CH_TEXT_WIDTH > MAX_CH_TEXT_WIDTH
0177         CH_TEXT_WIDTH = MAX_CH_TEXT_WIDTH;
0178     end
0179     if GAIN_EDIT_WIDTH > MAX_GAIN_EDIT_WIDTH
0180         GAIN_EDIT_WIDTH = MAX_GAIN_EDIT_WIDTH;
0181     end
0182     if GAIN_UNIT_TEXT_WIDTH > MAX_GAIN_UNIT_TEXT_WIDTH
0183         GAIN_UNIT_TEXT_WIDTH = MAX_GAIN_UNIT_TEXT_WIDTH;
0184     end
0185 
0186     %
0187     % --- Create edit components
0188     %
0189     itemNum = 0;
0190 
0191     ITEM_LEFT_BEGIN = FRAME_LEFT_MARGIN + FRAME_INNER_LEFT_MARGIN;
0192     ITEM_BOTTOM_BEGIN  = 1.0 - (FRAME_TOP_MARGIN + FRAME_INNER_TOP_MARGIN);
0193 
0194     for k=1:HORIZONTAL_ITEM_NUM
0195         for j=1:VERTICAL_ITEM_NUM
0196             ITEM_LEFT   = ITEM_LEFT_BEGIN + (k-1) * ITEM_WIDTH;
0197             ITEM_BOTTOM = ITEM_BOTTOM_BEGIN - (j * ITEM_HEIGHT);
0198             % Channel name
0199             channel = channels{itemNum+1};
0200             if isnumeric(channel)
0201                 channel = num2str(channel);
0202             end
0203             uicontrol('String', ['ch:' channel ' '], 'style', 'text', 'units', 'normalized', ...
0204                         'HorizontalAlignment', 'right', ...
0205                         'parent', fig,...
0206                         'position',[ITEM_LEFT, ITEM_BOTTOM, ...
0207                                     CH_TEXT_WIDTH, ITEM_HEIGHT]);
0208             % Gain input edit box
0209             gain_str = num2str(gain_list{itemNum+1});
0210             if strcmp(gain_str, '0')
0211                 gain_str = '';
0212             end
0213             uicontrol( 'style', 'edit', 'units', 'normalized', ...
0214                         'HorizontalAlignment', 'right', ...
0215                         'parent', fig,...
0216                         'String', gain_str,...
0217                         'Tag', [channel '_edit'],...
0218                         'TooltipString', GAIN_EDIT_TOOL_TIP_STRING,...
0219                         'callback', 'gain_input(''callback'', gcbo)',... 
0220                         'position',[ITEM_LEFT+CH_TEXT_WIDTH, ITEM_BOTTOM, ...
0221                                     GAIN_EDIT_WIDTH, ITEM_HEIGHT]);
0222             % [uV/V] text
0223             uicontrol('String', [' ', UNIT_STRING], 'style', 'text', 'units', 'normalized', ...
0224                         'HorizontalAlignment', 'left', ...
0225                         'parent', fig,...
0226                         'position',[ITEM_LEFT+CH_TEXT_WIDTH+GAIN_EDIT_WIDTH, ITEM_BOTTOM, ...
0227                                     GAIN_UNIT_TEXT_WIDTH, ITEM_HEIGHT]);
0228             itemNum = itemNum + 1;
0229             if itemNum >= length(channels)
0230                 break;
0231             end
0232         end
0233     end
0234 
0235 function callback(h, hObj)
0236 % This function processes user input
0237 %
0238 % [IN]
0239 %       h : figure handle
0240 %    hObj : component handle(action object handle)
0241 %
0242 
0243     H = guihandles(h);
0244 
0245     switch(hObj)
0246         case H.input_all_push
0247             gain_value = str2num(get(H.input_all_edit, 'String'));
0248             [result, err_msg] = Is_gain_value_OK(gain_value);
0249             if result == false
0250                 errordlg(err_msg, 'error');
0251                 return;
0252             end
0253             res = questdlg('Input gain to all channels?', 'confirm', 'Yes', 'No', 'Yes');
0254             if strcmp(res, 'Yes')
0255                 gain_all_str = get(H.input_all_edit, 'String');
0256                 h_editboxes = findobj(h, 'ToolTipString', GAIN_EDIT_TOOL_TIP_STRING);
0257                 set(h_editboxes, 'String', gain_all_str);
0258             end
0259         case H.input_all_edit
0260 %             gain_value = str2num(get(H.input_all_edit, 'String'));
0261 %             if Is_gain_value_OK(gain_value)
0262 %                 set(H.input_all_push, 'Enable', 'on');
0263 %             else
0264 %                 set(H.input_all_push, 'Enable', 'off');
0265 %             end
0266         case H.ok_push
0267             [eeg_gain] = get_gain_data_from_editbox(h);
0268 %            [result, ch_no] = has_empty_input(eeg_gain);
0269             result = false;
0270             if result
0271                 errordlg(['ch', num2str(ch_no(1)), ' : gain value is empty.'], 'error');
0272             else
0273                 set_application_status(h, VALUE_RETURN_EXIT);
0274                 set_application_data(h, eeg_gain);
0275                 escape_waitfor(h);
0276             end
0277         case H.cancel_push
0278             set_application_status(h, VALUE_NO_RETURN_EXIT);
0279             closereq;
0280         otherwise
0281             % This is gain edit box callback
0282             str_tool_tip = get(hObj, 'ToolTipString');
0283             if strcmp(str_tool_tip, GAIN_EDIT_TOOL_TIP_STRING)
0284                 str_tag = get(hObj, 'Tag');
0285                 [channel, count, errmsg] = sscanf(str_tag, '%[^_]_edit');
0286                 if ~isempty(errmsg)
0287                     error('gain_input application error.');
0288                 end
0289                 gain_value = str2num(get(hObj, 'String'));
0290                 [result, err_msg] = Is_gain_value_OK(gain_value);
0291                 if result == false
0292                     % Invalid value
0293                     errordlg(['ch' num2str(channel) ' : ', err_msg], 'error');
0294                     set(hObj, 'String', '');
0295                 else
0296                     set(hObj, 'String', num2str(gain_value));
0297                 end
0298             end
0299     end
0300 
0301 function [result, err_msg] = Is_gain_value_OK(gain_value)
0302 % This function checks gain value which is collect or not.
0303 %
0304 % [IN]
0305 %    gain_value : gain value.
0306 % [OUT]
0307 %    result : true  % gain value is collect.
0308 %             false % gain value is illigal.
0309 %    err_msg: illigal reason.
0310 %
0311    result = false;
0312    err_msg = [];
0313 
0314 %   if isempty(gain_value)
0315 %       err_msg = 'Inputted value is invalid.';
0316 %   else
0317    if gain_value == 0
0318        err_msg = '0 cannot be specified.';
0319    else
0320        result = true;
0321    end
0322 
0323 function escape_waitfor(h)
0324 % Call this function, escape 'waitfor' loop in 'init' processing.
0325 % In 'init' processing, use 'waitfor' MATLAB function.
0326 % [IN]
0327 %    fig : figure handle
0328 
0329     set(h, 'Name', 'close');  % Waitfor exit by this changing.
0330 
0331 function set_application_status(h, status)
0332 % This function sets application status
0333 % [IN]
0334 %         h : figure handle
0335 %    status : VALUE_RETURN_EXIT or VALUE_NO_RETURN_EXIT
0336 
0337     % load current data
0338     s = guidata(h);
0339 
0340     % set status
0341     s.status = status;
0342 
0343     % save data
0344     guidata(h, s);
0345 
0346 function [status] = get_application_status(h)
0347 % This function returns application status.
0348 % [IN]
0349 %    h : figure handle
0350 % [OUT]
0351 %    status : VALUE_RETURN_EXIT or VALUE_NO_RETURN_EXIT
0352 
0353     % load current data
0354     s = guidata(h);
0355 
0356     % return status
0357     status = s.status;
0358 
0359 function set_application_data(h, gain_data)
0360 % This function sets application data to figure.
0361 %
0362 % [IN]
0363 %            h : figure handle
0364 %    gain_data : gain_data(:, 1) : Nx1 channel list
0365 %                gain_data(:, 2) : Nx1 gain value list
0366 
0367     % load current data
0368     s = guidata(h);
0369 
0370     % set gain_data
0371     s.data.gain_data = gain_data;
0372     
0373     % save data
0374     guidata(h, s);
0375 
0376 function [gain_data] = get_application_data(h)
0377 % This function returns application data.
0378 %
0379 % [IN]
0380 %    h : figure handle
0381 % [OUT]
0382 %    gain_data : gain_data(:, 1) : Nx1 channel list
0383 %                gain_data(:, 2) : Nx1 gain value list
0384 %
0385     % load current data
0386     s = guidata(h);
0387 
0388     % set gain data
0389     gain_data = s.data.gain_data;
0390 
0391 function [gain] = get_gain_data_from_editbox(h)
0392 % This function returns gain data which editboxes have.
0393 %
0394 % [IN]
0395 %    h : figure handle
0396 % [OUT]
0397 %    gain : gain data
0398 %
0399 %
0400     [gain] = get_application_data(h);
0401 
0402     for k=1:length(gain(:, 1))
0403         % get editbox object
0404         child_h = findobj(h, 'Tag', [num2str(gain{k, 1}) '_edit']);
0405 
0406         % get gain value
0407         value = str2num(get(child_h, 'String'));
0408         if ~isempty(value)
0409             gain{k, 2} = value;
0410         else
0411             gain{k, 2} = 0;
0412         end
0413     end
0414 
0415 function [result, ch_no] = has_empty_input(gain)
0416 % This function check gain array and
0417 % returns channel number which is not inputted gain.
0418 % [IN]
0419 %    gain : gain(:, 1) Nx1 channel number list
0420 %           gain(:, 2) Nx1 gain list
0421 % [OUT]
0422 %    result : true  % There is empty input.
0423 %             false % There is no empty input.
0424 %     ch_no : empty channel number.
0425 
0426     result = false;
0427     ch_no  = [];
0428 
0429     for k=1:length(gain(:, 1))
0430         if gain(k, 2) == 0
0431             ch_no = [ch_no;gain(k, 1)];
0432             result = true;
0433         end
0434     end
0435 
0436     ch_no = ch_no';
0437 
0438 %
0439 % --- Constant Definition
0440 %
0441 function [value] = VERTICAL_ITEM_NUM
0442     value = 20;
0443 function [value] = VALUE_RETURN_EXIT
0444     value = 0;
0445 function [value] = VALUE_NO_RETURN_EXIT
0446     value = 1;
0447 function [str] = GAIN_EDIT_TOOL_TIP_STRING
0448     str = 'gain';
0449 function [str] = UNIT_STRING
0450     str = '[V/V]';
0451 function [str] = USAGE_STRING
0452     str = 'Usage : [eeg_gain, cancelled] = gain_input(''init'', channel_list [,gain_list]);';

Generated on Tue 27-Aug-2013 11:46:04 by m2html © 2005