0001 function [varargout] = gain_input(varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
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
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
0068 waitfor(h, 'Name', 'close');
0069 if ~ishandle(h)
0070
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
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
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
0102
0103
0104
0105
0106
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
0115
0116
0117
0118
0119
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
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
0137 set(H.unit_text, 'String', UNIT_STRING);
0138
0139
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
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
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
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
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
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
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
0237
0238
0239
0240
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
0261
0262
0263
0264
0265
0266 case H.ok_push
0267 [eeg_gain] = get_gain_data_from_editbox(h);
0268
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
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
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
0303
0304
0305
0306
0307
0308
0309
0310
0311 result = false;
0312 err_msg = [];
0313
0314
0315
0316
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
0325
0326
0327
0328
0329 set(h, 'Name', 'close');
0330
0331 function set_application_status(h, status)
0332
0333
0334
0335
0336
0337
0338 s = guidata(h);
0339
0340
0341 s.status = status;
0342
0343
0344 guidata(h, s);
0345
0346 function [status] = get_application_status(h)
0347
0348
0349
0350
0351
0352
0353
0354 s = guidata(h);
0355
0356
0357 status = s.status;
0358
0359 function set_application_data(h, gain_data)
0360
0361
0362
0363
0364
0365
0366
0367
0368 s = guidata(h);
0369
0370
0371 s.data.gain_data = gain_data;
0372
0373
0374 guidata(h, s);
0375
0376 function [gain_data] = get_application_data(h)
0377
0378
0379
0380
0381
0382
0383
0384
0385
0386 s = guidata(h);
0387
0388
0389 gain_data = s.data.gain_data;
0390
0391 function [gain] = get_gain_data_from_editbox(h)
0392
0393
0394
0395
0396
0397
0398
0399
0400 [gain] = get_application_data(h);
0401
0402 for k=1:length(gain(:, 1))
0403
0404 child_h = findobj(h, 'Tag', [num2str(gain{k, 1}) '_edit']);
0405
0406
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
0417
0418
0419
0420
0421
0422
0423
0424
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
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]);';