No BSD License  

Highlights from
simbus

from simbus by Sanjai Singh
The DataBus Blockset provides a method for manipulating signals from a vector in SIMULINK.

busmuxui(action,argument1)
function busmuxui(action,argument1)
%BUSMUXUI GUI Interface for selecting data off a bus line.
%
%Example:
% busmuxui('Init')
%
%Brings up the gui.  Enter the string:
%
% eval('busmuxui(''Init'')')
%
%in the dialog strings of the text based Selector block to use the gui.
%Be sure to name the Masked mux block "Mux" before grouping and masking it.

%William York, 11/95.
%Copyright (c) 1995 by The MathWorks, Inc.

%$Revision: 1.1 $ $Date: 1997/02/11 20:50:10 $
Version = version;
v5 = strcmp(Version(1),'5');
FigureTag=[];
Action=[];
if (nargin > 2)
  error('Too many input arguments to busmuxui.')
end
if (nargin > 1)
  FigureTag=argument1;
end  
if (nargin > 0)
  Action=action;
end  
if (nargin == 0)
  error('Not enough input arguements to busmuxui.')
end

if (strcmp(Action,'Init'))
  % Get the name of the current block
  blockname=gcb;
  % Get a handle to the current block
  h_cb=get_param(blockname,'Handle');
  % Check if figure already exists
  h_fig=findobj('Tag',blockname);
  if (~isempty(h_fig))
    figure(h_fig)
    return
  end
  % Get the parameters from the underlying Mux system
  h_mux=get_param([blockname '/Mux'],'Handle');
  entries=mask2str(h_mux);
  % The mask should always contain at least these 4 entries, In this order
  OutputBusName=deblank(strrep(entries(1,:),'''',''));
  InputBusNames=strrep(deblank(strrep(entries(2,:),'''','')),'|',':');
  NamesFlag=str2num(deblank(strrep(entries(3,:),'''','')));
  [Strings,Sizes,str]=busread(OutputBusName);
  if (isempty(Strings))
    errstr=str2mat('Error loading bus file:',['- ' str]);
    errordlg(errstr,[blockname ' Error']);
    error(errstr)
  end  

  layout;  % This calls a script to setup some default spacing variables
  ScreenS=get(0,'ScreenSize');
  % Default spacing
  VerticalSpace=5;
  % Number of rows
  NumPush=1;
  NumBlank=1;
  NumText=6;
  NumObjects=( NumPush + NumBlank + NumText );
  % Figure positioning
  FigH=NumObjects*mStdButtonHeight + (NumObjects - 1)*VerticalSpace + 2*mFrameToText;
  FigW=(4*mStdButtonWidth + 2*mFrameToText);
  FigurePos=[(ScreenS(3)-FigW)/2 (ScreenS(4)-FigH)/2 FigW FigH];
  DefUIBgC = get(0,'DefaultUIControlBackgroundColor');
  % Place figures and UiControls  
  % Check if h_fig references a valid figure
  if (~isempty(h_fig) & ishandle(h_fig))
    set(h_fig,'Pos',FigurePos)
  else
  % Figure handle does not exist, create a new one
  h_fig=figure('Visible','off','Units','pixels','Pos',FigurePos, ...
      'Color',DefUIBgC,'NumberTitle','off','Name',blockname,...
      'Tag',blockname,'UserData',h_cb);
  if v5
    set(h_fig, 'Interruptible', 'on');
  else  
    set(h_fig, 'Interruptible', 'yes');
  end  
end

  % Place push buttons
  Ind=1;
  Mul=NumObjects - NumBlank - NumText - Ind;  
  ApplyCallback='busmuxui(''Apply'',get(gcf,''Tag''))';
  h_push=uicontrol('Visible','off','Parent',h_fig,'Units','pixels', ...
  'Enable','off','String','Apply','pos',[ ...
    mFrameToText
    mFrameToText + Mul*VerticalSpace + Mul*mStdButtonHeight
    mStdButtonWidth*.9
    mStdButtonHeight
  ], 'Style','push','Tag','ApplyButton','CallBack',ApplyCallback );
  if v5
    set(h_push, 'Interruptible', 'on')
  else
    set(h_push, 'Interruptible', 'yes')
  end

CloseCallback='busmuxui(''Close'',get(gcf,''Tag''))';
  h_push=uicontrol('Visible','off','Parent',h_fig,'Units','pixels', ...
  'Enable','on','String','Close','pos',[ ...
    FigW - mFrameToText - mStdButtonWidth*.9
    mFrameToText + Mul*VerticalSpace + Mul*mStdButtonHeight
    mStdButtonWidth*.9
    mStdButtonHeight
  ], 'Style','push','Tag','CloseButton','CallBack',CloseCallback);
  if v5
    set(h_push, 'Interruptible', 'on')
  else
    set(h_push, 'Interruptible', 'yes')
  end

  % Blank space goes here

  % Place text boxes
  Ind=1;
  Mul=NumObjects - Ind;
  h_text=uicontrol('Visible','off','Parent',h_fig,'Units','pixels', ...
    'BackgroundColor',DefUIBgC, 'pos',[ ...
    mFrameToText
    mFrameToText + Mul*VerticalSpace + Mul*mStdButtonHeight
    4*mStdButtonWidth
    mStdButtonHeight
    ], 'Style','text','String','Output Bus Name:','HorizontalAlignment','left');

  Ind=2;
  Mul=NumObjects - Ind;
  EditCallback=[ 'busmuxui(''EnableApply'',get(gcf,''Tag''))' ];
  h_edit=uicontrol('Visible','off','Parent',h_fig,'Units','pixels', ...
    'Callback',EditCallback,'BackgroundColor','white', 'pos',[ ...
    mFrameToText
    mFrameToText + Mul*VerticalSpace + Mul*mStdButtonHeight
    4*mStdButtonWidth
    mStdButtonHeight
    ], 'Style','edit','HorizontalAlignment','left','Tag','OutputBusName','String',OutputBusName);

  Ind=3;
  Mul=NumObjects - Ind;
  h_text=uicontrol('Visible','off','Parent',h_fig,'Units','pixels', ...
    'BackgroundColor',DefUIBgC, 'pos',[ ...
    mFrameToText
    mFrameToText + Mul*VerticalSpace + Mul*mStdButtonHeight
    4*mStdButtonWidth
    mStdButtonHeight
    ], 'Style','text','String','Input Bus Names (separated by :):','HorizontalAlignment','left');

  Ind=4;
  Mul=NumObjects - Ind;
  EditCallback=[ 'busmuxui(''EnableApply'',get(gcf,''Tag''))' ];
  h_edit=uicontrol('Visible','off','Parent',h_fig,'Units','pixels', ...
    'Callback',EditCallback,'BackgroundColor','white', 'pos',[ ...
    mFrameToText
    mFrameToText + Mul*VerticalSpace + Mul*mStdButtonHeight
    4*mStdButtonWidth
    mStdButtonHeight
    ], 'Style','edit','HorizontalAlignment','left','Tag','InputBusNames','String',InputBusNames);

  Ind=5;
  Mul=NumObjects - Ind;
  h_text=uicontrol('Visible','off','Parent',h_fig,'Units','pixels', ...
    'BackgroundColor',DefUIBgC, 'pos',[ ...
    mFrameToText
    mFrameToText + Mul*VerticalSpace + Mul*mStdButtonHeight
    4*mStdButtonWidth
    mStdButtonHeight
    ], 'Style','text','String','Show bus names (0 or 1):','HorizontalAlignment','left');

  Ind=6;
  Mul=NumObjects - Ind;
  EditCallback=[ 'busmuxui(''EnableApply'',get(gcf,''Tag''))' ];
  h_edit=uicontrol('Visible','off','Parent',h_fig,'Units','pixels', ...
    'Callback',EditCallback,'BackgroundColor','white', 'pos',[ ...
    mFrameToText
    mFrameToText + Mul*VerticalSpace + Mul*mStdButtonHeight
    4*mStdButtonWidth
    mStdButtonHeight
    ], 'Style','edit','HorizontalAlignment','left','Tag','NamesFlag','String',NamesFlag);

  % Make figure visible
  set(findobj(h_fig),'visible','on')
% end Action == Init
elseif (strcmp(Action,'Close'))
  if (isempty(FigureTag))
    error(['Action ' Action ' requires a second argument - the Figure tag'])
  end

  % Get handle to figure given tag
  h_fig=findobj('Tag',FigureTag);

  % Apply any pending changes if necessary
  if(strcmp(lower(get(findobj('Parent',h_fig,'Tag','ApplyButton'),'Enable')),'on'))
    busmuxui('Apply',FigureTag);
    % Check if the figure got closed
    if (~ishandle(findobj('Tag',FigureTag)))
      return
    end
  end

  % Delete the figure and its children
  delete(flipud(findobj('Tag',FigureTag)))
% end Action == Close
elseif (strcmp(Action,'Apply'))
  if (isempty(FigureTag))
    error(['Action ' Action ' requires a second argument - the Figure tag'])
  end

  % Get handle to figure given tag
  h_fig=findobj('Tag',FigureTag);

  % Get the name of the input bus files from the dialog
  h_input_names=findobj('Parent',h_fig,'Tag','InputBusNames');
  InputBusNames=strrep(get(h_input_names,'String'),':','|');

  % Get the name of the output bus files from the dialog
  h_output_name=findobj('Parent',h_fig,'Tag','OutputBusName');
  OutputBusName=get(h_output_name,'String');

  % Get the names flag from the dialog
  h_names_flag=findobj('Parent',h_fig,'Tag','NamesFlag');
  NamesFlag=str2num(get(h_names_flag,'String'));

  % Get the handle to the current block
  h_cb=get(h_fig,'UserData');
  TestString='sys=get_param(h_cb,''Parent'');';
  ErrorString='sys=[];';
  eval(TestString,ErrorString);
  if (isempty(sys))
    disp('Block diagram no longer exists - closing selector.')
    delete(flipud(findobj('Tag',FigureTag)))
    return
  end
  cb=get_param(h_cb,'Name');

  % Get the handle to the underlying Mux block
  h_mux=get_param([sys '/' cb '/Mux'],'Handle');

  dbimdraw(h_cb,NamesFlag,OutputBusName,InputBusNames,0);

  % Verify the bus definition files
  busmux(OutputBusName,InputBusNames);

  % Update the Mask
  NewMaskEntry=str2mat(['''' OutputBusName ''''],['''' InputBusNames ''''],num2str(NamesFlag));
  str2mask(h_mux,NewMaskEntry);

  % Disable Apply button
  set(findobj('Parent',h_fig,'Tag','ApplyButton'),'Enable','off')
% end Action == Apply
elseif (strcmp(Action,'EnableApply'))
  if (isempty(FigureTag))
    error(['Action ' Action ' requires a second argument - the Figure tag'])
  end

  % Get handle to figure given tag
  h_fig=findobj('Tag',FigureTag);

  % Enable the Apply button
  set(findobj('Parent',h_fig,'Tag','ApplyButton'),'Enable','on')  
% end Action == EnableApply
else
  error(['Invalid action for busmuxui.  Action was "' Action '"'])
end    


% EOF busmuxui.m  

Contact us at files@mathworks.com