function commgui(name, fileload)
%COMMGUI A MATLAB GUI example of bit-error rate computation.
%
%WARNING: This is an obsolete function and may be removed in the future.

%%    Original design by Wes Wang, 1996                    %
%%    Last updated by Jun Wu, Mar-07 1997                  %
%%    Copyright 1996-2000 The MathWorks, Inc.
% $Revision: 1.26 $

% if the comment's sign more than 2 like %%%, it's only for debug.
% Assign the name of figure 'COMMGUI'
if nargin < 1
   name = 'Communications error rate estimation';
end;

% search if this exec exist.
fig_exist = get(0, 'child');
main_figure = 0;
if ~isempty(fig_exist)
   i = 1;
   while (~main_figure & i <= length(fig_exist))     %check out all existing figure
      if strcmp(get(fig_exist(i), 'name'), name)     %compare each figure's name with 'name'
         main_figure = fig_exist(i);                 %if it exists, we set main_figure--1, otherwise it is --0.
      end;
      i = i + 1;        
   end;
end;

%Open the figure if it didn't exist
if ~main_figure
   % create a figure
   default_color = get(0, 'defaultuicontrolbackgroundcolor');
   figcolor = [.8 .8 .8];
   main_figure = figure(...
      'Name', name, ...
      'NumberTitle', 'off', ...
      'Units', 'normalized', ...
      'Position', [.03, .25,  .6, .56],...
      'Visible','off',...
      'Menubar', 'none', ...
      'color', figcolor ...
      );
   
   basic_color = [1 1 1];
   advan_color = (default_color + basic_color) /2;
   
   if strcmp(get(0,'showhidd'),'on')
      set(0,'showhidd','off');
   end;
   
   %Create couple sets of UIControl buttons
   popup_name_list = [...
         'Source      ';...
         'Err-Ctr Code';...
         'Modulation  ';...
         'Channel     '];
   
   popup_text_list = [...
      'Rand Int   ';...              %1 \   popup_number = [3; 11; 20; 25; 27]; 
      'Read Var   ';...              %2  >->  for 'Source' (#1--#3)
      'My Own     ';...              %3 /
      'None       ';...              %4 \  
      'Hamming    ';...              %5  \
      'Linear     ';...              %6   \
      'Cyclic     ';...              %7    \  for 'Err-Ctr Code'(#4--#11) 
      'BCH        ';...              %8    /
      'R-S        ';...              %9   /
      'Convolution';...              %10 /
      'My Own     ';...              %11/
      'None       ';...               %12 \
      'ASK        ';...               %13  \
      'QASK/sq    ';...               %14   \
      'QASK/cir   ';...               %15    \
      'QASK/arb   ';...               %16     >--> for 'Modulation' (#12--#20)
      'FSK        ';...               %17    / 
      'PSK        ';...               %18   /      
      'MSK        ';...               %19  /
      'My Own     ';...               %20 /
      'None       ';...              %21 \
      'AWGN       ';...              %22  \
      'Uniform    ';...              %23   >--> for 'Channel' (#23--#27) 
      'Binary     ';...              %24  /
      'My Own     ';...              %25 /
      'Passband   ';...              %26 \
      'Baseband   '];                %27 /
   
   entry_text_list = [...
      'Sample Freq ';...           %1 \
      'Data Length ';...           %2  \  under 'Source' (#1--#4)
      'M-ary Number';...           %3  /
      'Seed        ';...           %4 /
      'CdWrd Length';...           %5 \
      'Msg Length  ';...           %6  \under 'Err-Ctr Code' (#5--#8)
      'GeneratePoly';...           %7  /
      'None        ';...           %8 /
      'ModSmp Freq ';...          % 9 \
      'NumberOn Cir';...          %10  \ 
      'RadiiOn Cir ';...          %11   \
      'PhaseOn Cir ';...          %12    \
      'Mean        ';...          %13     \
      'Var         ';...          %14      >---> under 'Modulation'(#9--#20)
      'Seed        ';...          %15     /
      'None        ';...          %16    /
      'Phase Offset';...          %17   / 
      'Flt Numerate';...          %18  /
      'Flt Denomin ';...          %19 /
      'Carrier Freq'];            %20/
   
   %popup_number is to load string into each popupmenu
   popup_number = [0; 3; 11; 20; 25; 27];
   line_pos = [.01, 1/5+.01 2/5+.01 4/5-.01, 3/5-.01];
   
   h_axes = axes('position', [0 0 1 1], ...
                 'Nextplot', 'add', ...
                 'Visible', 'off');
   h_plot = plot( 0, 0, 'w-', 'Parent', h_axes, 'Erase', 'xor');
   for i = 1:5
      if i == 3
         set(h_plot, 'XData', [get(h_plot, 'XData'), ...
                  (line_pos(i) - .005) + [NaN 0 0 2/5-.03 2/5-.03 0]], ...
            'YData', [get(h_plot, 'YData'), .44+[NaN 0 .51 .51 0 0]],...
            'EraseMode', 'Xor');
         textl(i) = text(line_pos(i)+.135, .93, deblank(popup_name_list(i,:)));
         set(textl(i),...
            'FontUnits','normalized',...
            'Fontsize', 0.0235);
      elseif i == 5
         textl(i) = 0;
      else
         set(h_plot, 'XData', [get(h_plot, 'XData'), ...
                       (line_pos(i)-.005)+[NaN 0 0 1/5-.02 1/5-.02 0]],...
            'YData', [get(h_plot, 'YData'), .44+[NaN 0 .51 .51 0 0]], ...
            'EraseMode', 'Xor','Color','black');
         if i == 1
            % 'Source'
            textl(i) = text(line_pos(i)+.05, .93, deblank(popup_name_list(i,:)));
         elseif i == 2
            % 'Err-Ctr Code'
            textl(i) = text(line_pos(i)+.035, .93, deblank(popup_name_list(i,:)));
         else
            % 'Channel'
            textl(i) = text(line_pos(i)+.045, .93, deblank(popup_name_list(i,:)));
         end;
         set(textl(i),...
            'FontUnits','normalized',...
            'Fontsize', 0.0235);
      end;
      
      % create 5 popup-menu bar to change input variables
      popup_menu(i) = uicontrol(...
         'Style', 'popupmenu',...
         'Units', 'normalized',...
         'Position', [line_pos(i), .86, 1/5-.03, .05],...
         'String', popup_text_list(popup_number(i)+1 : popup_number(i+1),:), ...
         'BackgroundColor', basic_color, ...
         'Callback', ['comermn(',num2str(i), ',', num2str(main_figure) ')']);
      
      % for the parameter entries under popup-menu bar
      for j = 1 : 4
         temp_indx = (i-1)*4 + j;
         entr_text(temp_indx) = text(line_pos(i), .82-.1*(j-1), ...
            entry_text_list((i-1)*4+j,:), ...
            'Visible', 'off', ...
            'FontUnits','normalized',...
            'Fontsize', 0.02);
         entr_valu((i-1)*4+j) = uicontrol(...
            'Style','edit',...
            'Visible', 'off',...
            'Units','normalized',...
            'BackgroundColor', basic_color,...
            'Position', [line_pos(i), .75-.1*(j-1), 1/5-.03, .05]);
      end;
   end;

   %set up the parameter value entry
   %
   %Under <Source>
   set(entr_valu(1:2), 'Visible', 'on', 'String', '100');
   set(entr_valu(3),   'Visible', 'on', 'String', '16');
   set(entr_valu(4),   'Visible', 'on', 'String', '12345');
   %Under < >
   set(entr_valu(14 : 15),              'String', '''default''');
   set(entr_valu(17),                   'String', '0');
   %turn on the axes --under <Source>
   set(entr_text(1:4), 'Visible','on');
   
   %set up User operation Interface, 
   %including 'Run', 'Close', 'Info' and 'Plot' buttons.
   % set up the functionality of 'Run' button
   exec(1) = uicontrol(...
      'Style','pushbutton',...
      'Units','normalized',...
      'Position',[.01, .01, 2/3*(1/6-.03), .05],...
      'String', 'Run',...
      'BackGroundColor', default_color );
   set(exec(1), 'Callback', ...
      ['set(',num2str(exec(1), 20), ',''Visible'',''on'');',...
         'comtmpo;','commod(', num2str(main_figure), ')']);
   
   % set up the functionality of 'Close' button
   exec(2) = uicontrol(...
      'Style','pushbutton',...
      'Units','normalized',...
      'Position',[0.12+.01, .01, 2/3*(1/6-.03), .05],...
      'String', 'Close',...
      'BackGroundColor', default_color, ...
      'Callback', ['set(0,''Showhidden'',''on'');',...
	  'close(', num2str(main_figure) ')']);

   % set up the functionality of 'Plot' button
   exec(3) = uicontrol(...
      'Style','pushbutton',...
      'Units','normalized',...
      'Position',[0.36+.01, .01, 2/3*(1/6-.03), .05],...
      'String', 'Plot',...
      'Enable', 'on',...
      'BackGroundColor', default_color, ...
      'Callback', ['complot(', num2str(main_figure) ')']);

   % set up the functionality of Matlab 'Info' button
   exec(13) = uicontrol(...
      'Style','pushbutton',...
      'Units','normalized',...
      'Position',[.24+.01, .01, 2/3*(1/6-.03), .05],...
      'String', 'Info',...
      'Enable', 'on',...
      'BackGroundColor',default_color, ...
      'Callback', 'hthelp commfunc');

   % set up the 'Curve fit order' window
   % the axes position's numbers are from GUIDE.
   %exec(4) = uicontrol(...
   %   'Style', 'text', ...
   %   'BackGroundColor', default_color, ...
   %   'Units', 'normalized', ...
   %   'Fontunits', 'normal', ...
   %  'Position',[0.44 0.056 0.190625 0.04], ...
   %   'String', 'Curve fit order',...
   %   'FontSize', .04);
   exec(4) = text(.5, .075, 1, 'Curve Fit Order');
   set(exec(4), ...
      'Fontunits', 'normal', ...
      'FontSize', .02);
   
   exec(5) = uicontrol(...
      'Style','edit',...
      'Units','normalized',...
      'Position',[0.5077 0.01 0.063047 0.0488],...
      'BackgroundColor', basic_color,...
      'String', '2');
   
   % setup 5 subplot windows to plot 'Bit-Error Rate vs Signal/Noise Ratio'.
   % now plot the sub-windows
   for k = 1:5
      exec(k+7) = axes(...
         'Position', [(k-1)*.19+.05  .175 .15 .15], ...
         'Tag',['Axes' num2str(k)],...
         'Nextplot', 'add',...
         'Units', 'normalized');
      
      yy = [0.0825  0.0388  0.0292  0.0069  0.0015  0.0012  0        0        0];
      xx = [9.1764 11.4483 15.5332 22.2081 33.8707 48.4043 86.8354 203.2098 827.2997];
      [x1, st] = sort(xx);
      y1 = yy(st);
      if k == 1
         plot(x1, y1, 'k*-',...
                'parent', exec(k+7));
         set(exec(k+7), 'title', ...
            text('String','Current Data', 'FontUnits', ...
                 'normal','fontsize',.135,'color','k'));
      elseif k == 2
         plot(x1, y1, 'rx-',...
                'parent', exec(k+7));
         set(exec(k+7), 'title', ...
            text('String','Record 1','FontUnits', 'normal','fontsize',.135,'color','r'));
      elseif k == 3
         plot(x1, y1, 'b+-',...
                'parent', exec(k+7));
         set(exec(k+7), 'title', ...
            text('String','Record 2','FontUnits', 'normal','fontsize',.135,'color','b'));
      elseif k == 4
         plot(x1, y1, 'mo-',...
                'parent', exec(k+7));
         set(exec(k+7), 'title', ...
            text('String','Record 3','FontUnits', 'normal','fontsize',.135,'color','m'));
      elseif k == 5
         plot(x1, y1, 'gh-',...
                'parent', exec(k+7));
         set(exec(k+7), 'title', ...
            text('String','Record 4','FontUnits', 'normal','fontsize',.135,'color','g'));
      end;
      set(exec(k+7),...
         'XTick', [], 'YTick', [],...
         'XLabel', text('String','S/N Ratio',...
                        'FontUnits','normalized',...
                        'Fontsize', 0.13 ),...
         'YLabel', text('String','Bit-Err Rate',...
                        'FontUnits','normalized',....
                        'Fontsize', 0.13 ),...
         'Xscale', 'log',...
         'Yscale', 'log');
      if k == 2 | k == 3 | k == 4
         aaa=get(exec(k+7), 'Child');
         set(findobj(aaa,'type','line'), 'XData', [], 'Ydata', []);
      end;
   end; 
   
   % To create a UI menu for the GUI, it supports 'Load', 'Save', 'Close' and 'Help'
   load_save(1) = uimenu(main_figure,'Label', 'File');
   uimenu(load_save(1),'Label','Load setup',...
      'Interrupt','on ',...
      'Call',  ['comload(', num2str(main_figure), ')']);
   uimenu(load_save(1),'Label','Save setup',...
      'Interrupt','on',...
      'Call',  ['comsave(', num2str(main_figure), ')']);
   uimenu(load_save(1),'Label','Close',...
      'Interrupt','on',...
      'Call',  ['close(', num2str(main_figure), ')']);
   load_save(1) = uimenu(main_figure,'Label','Help');
   uimenu(load_save(1),'Label','Communications Toolbox','Interrupt','on',...
      'Call',  'hthelp commhelp');
   uimenu(load_save(1),'Label','GUI for Communications Toolbox','Interrupt','on',...
      'Call',  'hthelp commfunc');
   
   % To draw a trash can by using axes instead of patch.
   xd = .96- [18  18  32 32  0  0 50 50 32 NaN  1 1 49 49 NaN 10 12 12 10 NaN ...
         20 22 22 20 NaN 30 32 32 30 NaN 40 42 42 40]*0.01*.09;
   yd = [95 100 100 95 95 90 90 95 95 NaN 90 0  0 90 NaN 80 75 15 10 NaN ...
         80 75 15 10 NaN 80 75 15 10 NaN 80 75 15 10]*0.01*.09 +0.001;
   exec(14) = axes('Position', [0.87  .008 .11 .1], ...
      'Tag', 'TrashCan', ...
      'Units', 'normalized', ...
      'NextPlot', 'add',...
      'Color', default_color);
   hold on;
   set(exec(14), 'Visible', 'off');
   set(gca, 'XTick', [], 'YTick', []);
   plot(xd,yd, 'parent', gca);
   grid off;
   hold off;
   exec(15) = text(.89, .04, 1, 'Trash', 'Parent', gca);
   set(exec(15), ...
      'Parent',h_axes,...
      'FontUnits', 'normalized',...
      'FontWeight', 'bold',...
      'Fontsize', .024);

   % To make a interface between this GUI and matlab workspace.
   exec(6) = axes('Position', [0.665  .0095 .12 .1], ...
      'Tag', 'To/From Workspace', ...
      'NextPlot', 'add',...
      'Units', 'normal');
   yyy=[3.2 4 nan 3.2 4 nan 3.2 4 nan 3.2 3.2 0 4 nan 0 4 0 0 nan 4 4];
   xxx=[3   3 nan 2   2 nan 1   1 nan 0   4   4 4 nan 0 0 0 4 NaN 0 4];
   plot(xxx,yyy,'b', 'parent', exec(6));
   set(exec(6),'visible','off');
   exec(7) = text(.67, .04, 1, '>>Workspace');
   set(exec(7), ...
      'Parent',h_axes,...
      'FontUnits', 'normalized',...
      'Fontsize', .024, ...
      'Fontweight', 'b');
   % end of WorkSpace UI set up.
   
   % To avoid graphic flashing once you run the simulation, set the 'Erasemode' to 'Xor'
   % here, we display some message on the middle of the figure.
   load_save(2)=plot(0, 0,'w:','Parent',h_axes);
   set(load_save(2), 'EraseMode', 'Xor', 'Color', [.7 .7 .7]);
   bar_color(1) = text(.3, .39, 'Loading setup, please wait...');
   set(bar_color(1), ...
      'Parent', h_axes,...
      'Visible','off',...
      'FontUnits', 'normalized',...
      'FontSize', .035);
   bar_color(2) = patch([.01 .0101, .0101 0.01], [0.38 .38 .405 .405], 'r');
   set(bar_color(2),...
      'Parent', h_axes,...
      'EdgeColor', 'cyan',...
      'Visible', 'off', ...
      'EraseMode', 'Xor');
   
   % The following string will be displaying when GUI is running 
   % to report the running statues.
   run_flag(1) = text(3/7, .39, 'Source');
   run_flag(2) = text(3/7, .39, 'Coding');
   run_flag(3) = text(3/7, .39, 'Modulation');
   run_flag(4) = text(3/7, .39, 'Channel');
   run_flag(5) = text(3/7, .39, 'Demodulation');
   run_flag(6) = text(3/7, .39, 'Decoding');
   run_flag(7) = text(3/7, .39, 'Saving data');
   for k = 1:7
      set(run_flag(k), ...
         'Parent', h_axes, ...
         'Visible','off',...
         'FontUnits', 'normalized',...
         'FontSize', .027);
   end;
      
   user_data = [h_axes;...  % 1   (1)  -- main axes
      h_plot; ...           % 1   (2)  -- use for drawing the semi-hidden frame behind those popup-menus
      textl(:);...          % 5  (3-7) -- 'Source' + 'Err-Ctr Code' + 'Modulation' + 'Channel' + blank
      popup_menu(:);...     % 5  (8-12)--   1~3,         4~11,           12~20,        21~25,    26~27 (from popup_text_list)
      entr_text(:);...      % 20(13-32)--   1~4,         5~8,             9~20  (select text from entry_text_list)
      entr_valu(:);...      % 20(33-52)--  input the default data in editable box
      exec(:);...           % 15(53-67)-- 'Run'+'Close'+'Plot'+'CurveFitOrder'+CFO's bG+WS-pic+'WS'+5*plot+'Info'+trashcan+'Trash' 
      load_save(:);...      % 2 (68-69)-- menu + running_statues_show
      bar_color(:);...      % 2 (70-71)-- 'Loading ...' + BackGround
      run_flag(:)];         % 7 (72-78)-- to display the text in the middle of the figure while it's running
   
   tmp = get(main_figure, 'position');
   set(main_figure,...
      'UserData', user_data, ...
      'Visible', 'on', ...
      'Interruptible', 'on',...
      'WindowButtonDownfcn', 'combtnd(''main_figure'', 0)',...
      'HandleVisibility', 'callback');
     
   comload(main_figure, 1);
   set(main_figure,'Position',tmp ,'Units','normalized');
else
   set(0, 'currentfigure', main_figure);
end;  %%%last end

drawnow;

if nargin >= 2
   set(load_save(1), 'String', fileload, 'Color',[1 1 1]);
   comload(main_figure);
end;
   
% End of commgui.m

