0001 function pan6globals(varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 global gl currSys
0012 currPanel = varargin{1};
0013 panelDimens = get(currPanel, 'Position');
0014 backColor = get(currPanel, 'BackgroundColor');
0015 selBlocks = 0;
0016 enlargedFont = 0;
0017 currSys = '';
0018
0019 alltimers = timerfindall;
0020 stop(alltimers(1));
0021 set(alltimers(1),'TimerFcn',@ContinuousParamCheck,'Period',0.3);
0022 start(alltimers(1));
0023
0024
0025 function buildGlobalPanel(varargin)
0026
0027
0028 globBlock = find_system(bdroot,'SearchDepth',1,'BlockType','SubSystem','Tag','globals');
0029 if isempty(globBlock) || (~isempty(varargin) && isnumeric(varargin{3}) && varargin{3}==-1)
0030 gl = struct();
0031 updateGlobalBlock()
0032 else
0033 gl = get_param(globBlock{1},'UserData');
0034 end
0035
0036
0037
0038 panelDimens = get(currPanel, 'Position');
0039 delete(findobj(gcf,'Parent',currPanel))
0040 varnames = fieldnames(gl);
0041 y = numel(varnames);
0042
0043
0044 uicontrol('Parent',currPanel,'BackgroundColor',backColor,'Units','pixels','Position',[20 panelDimens(4)-40 panelDimens(3)-40 20],'String','List of global variables:','HorizontalAlignment','center','FontSize',enlargedFont+10,'FontWeight','bold','Visible','on','Style','text');
0045
0046 uicontrol('Parent',currPanel,'BackgroundColor',backColor,'Units','pixels','Position',[panelDimens(3)/2-150-20 panelDimens(4)-70 150 20],'String','Names:','HorizontalAlignment','right','FontSize',enlargedFont+9,'Visible','on','Style','text');
0047
0048 uicontrol('Parent',currPanel,'BackgroundColor',backColor,'Units','pixels','Position',[panelDimens(3)/2+20 panelDimens(4)-70 150 20],'String','Values:','HorizontalAlignment','left','FontSize',enlargedFont+9,'Visible','on','Style','text');
0049
0050 for i=1:y
0051
0052
0053
0054 uicontrol('Parent',currPanel,'Units','pixels','Position',[panelDimens(3)/2-200-20 panelDimens(4)-20-24*(i+2) 200 20],'String',['gl.',varnames{i}],'FontSize',enlargedFont+8,'Visible','on','Enable','on','UserData',i,'Callback',@globalName,'Style','edit');
0055
0056 currVal = gl.(varnames{i});
0057 if iscell(currVal), currVal = cell2str(currVal);
0058 else currVal = mat2str(currVal);
0059 end
0060 uicontrol('Parent',currPanel,'Units','pixels','Position',[panelDimens(3)/2+20 panelDimens(4)-20-24*(i+2) 200 20],'String',currVal,'FontSize',enlargedFont+8,'Visible','on','Enable','on','UserData',i,'Callback',@globalValue,'Style','edit');
0061
0062 uicontrol('Parent',currPanel,'Units','pixels','Position',[700 panelDimens(4)-20-24*(i+2) 20 20],'String','-','Visible','on','Enable','on','Callback', @RemoveGlobal,'UserData',i,'Style','pushbutton');
0063 end
0064
0065
0066 uicontrol('Parent',currPanel,'Units','pixels','Position',[panelDimens(3)/2-10 panelDimens(4)-50-24*(y+2) 20 20],'String','+','TooltipString','Add a global variable','Visible','on','Enable','on','Callback', @AddGlobal,'Style','pushbutton');
0067
0068
0069 uicontrol('Parent',currPanel,'Units','pixels','HorizontalAlignment','center','Position',[panelDimens(3)-100 10 80 16],'FontAngle','italic','FontSize',enlargedFont+8,'String','Clear all','TooltipString','Clear all the global variables','Visible','on','Callback',{@buildGlobalPanel,-1},'Style','pushbutton');
0070
0071
0072 end
0073
0074
0075
0076
0077 function ContinuousParamCheck(varargin)
0078
0079
0080
0081
0082 if isempty(bdroot) || strcmp(get_param(bdroot,'BlockDiagramType'),'library') || isempty(find_system(bdroot,'FindAll','on','Type','Annotation','Tag','ltpda model'))
0083 delete(findobj(gcf,'Parent',currPanel))
0084 currSys = gcs;
0085 gl = struct();
0086 return
0087 else
0088 if ~strcmp(gcs,currSys)
0089
0090 currSys = gcs;
0091 buildGlobalPanel();
0092 end
0093 end
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104 end
0105
0106
0107
0108
0109 function AddGlobal(varargin)
0110
0111 newGlob = 'global';
0112 ii = 1;
0113 while isfield(gl,[newGlob,num2str(ii)]), ii = ii+1; end
0114 newGlob = [newGlob,num2str(ii)];
0115 gl.(newGlob) = [];
0116 updateGlobalBlock()
0117 buildGlobalPanel()
0118
0119 end
0120
0121
0122
0123 function RemoveGlobal(hObject,varargin)
0124
0125 currGlob = get(hObject,'Userdata');
0126 varnames = fieldnames(gl);
0127 gl = rmfield(gl,varnames{currGlob});
0128 updateGlobalBlock()
0129 buildGlobalPanel()
0130
0131 end
0132
0133
0134
0135 function globalName(hObject,varargin)
0136
0137 currGlob = get(hObject,'Userdata');
0138 varnames = fieldnames(gl);
0139 newGlobName = get(hObject,'String');
0140 if numel(newGlobName)<4 || ~strcmp(newGlobName(1:3),'gl.'), newGlobName = ['gl.',strrep(newGlobName,'.','')]; end
0141
0142 newGlobName = ['gl.',genvarname(newGlobName(4:end))];
0143 if isempty(get(hObject,'String')) || isfield(gl,newGlobName(4:end)), set(hObject,'String',['gl.',varnames{currGlob}]); return; end
0144
0145 set(hObject,'String',newGlobName);
0146 gl = utils.prog.rnfield(gl,varnames{currGlob},newGlobName(4:end));
0147 updateGlobalBlock()
0148 buildGlobalPanel()
0149
0150 end
0151
0152
0153
0154 function globalValue(hObject,varargin)
0155
0156 currGlob = get(hObject,'Userdata');
0157 newValue = get(hObject,'String');
0158 varnames = fieldnames(gl);
0159 try newValue = eval(newValue); catch end
0160 gl.(varnames{currGlob}) = newValue;
0161 updateGlobalBlock()
0162 buildGlobalPanel()
0163
0164 end
0165
0166
0167
0168 function updateGlobalBlock(varargin)
0169
0170 varNames = fieldnames(gl);
0171
0172 if numel(varNames)==0
0173
0174 globBlock = find_system(bdroot,'BlockType','SubSystem','Tag','globals');
0175 if ~isempty(globBlock)
0176 delete_block(globBlock)
0177 return;
0178 end
0179 else
0180 globBlock = find_system(bdroot,'SearchDepth',1,'BlockType','SubSystem','Tag','globals');
0181 try globBlock = globBlock{1}; catch end
0182
0183 if isempty(globBlock)
0184 globBlock = add_block('ltpda_library/Commonly Used Blocks/Globals',[bdroot '/Globals']);
0185 annotation = find_system(bdroot,'FindAll','on','Type','Annotation','Tag','ltpda model');
0186 position = get_param(annotation,'Position');
0187 set_param(globBlock,'Position',[position(1)+20 position(2)-25 position(1)+150 position(2)-10 ])
0188 set_param(globBlock,'DropShadow','on')
0189 end
0190
0191
0192 set_param(globBlock,'UserData',gl)
0193 set_param(globBlock,'UserDataPersistent','on')
0194 end
0195
0196
0197 end
0198
0199
0200
0201 end