0001 function varargout = 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
0026
0027 function buildGlobalPanel(varargin)
0028
0029
0030 globBlock = find_system(bdroot,'SearchDepth',1,'BlockType','SubSystem','Tag','globals');
0031 if isempty(globBlock) || (~isempty(varargin) && isnumeric(varargin{3}) && varargin{3}==-1)
0032 gl = struct();
0033 else
0034 gl = get_param(globBlock{1},'UserData');
0035 end
0036
0037
0038
0039 panelDimens = get(currPanel, 'Position');
0040 delete(findobj(gcf,'Parent',currPanel))
0041 close(findobj('Tag','expandedEditField'))
0042 varnames = fieldnames(gl);
0043 y = numel(varnames);
0044
0045
0046 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');
0047
0048 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');
0049
0050 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');
0051
0052 for i=1:y
0053
0054 uicontrol('Parent',currPanel,'BackgroundColor',backColor,'Units','pixels','Position',[panelDimens(3)/2-200-20-15 panelDimens(4)-20-24*(i+2)-5 12 20],'String','gl.','FontSize',enlargedFont+8,'Visible','on','Enable','off','Style','text');
0055
0056 uicontrol('Parent',currPanel,'Units','pixels','Position',[panelDimens(3)/2-200-20 panelDimens(4)-20-24*(i+2) 200 20],'String',varnames{i},'FontSize',enlargedFont+8,'Visible','on','Enable','on','UserData',i,'Callback',@globalName,'Style','edit');
0057
0058 currVal = gl.(varnames{i});
0059 if iscell(currVal), currVal = cell2str(currVal);
0060 else currVal = mat2str(currVal);
0061 end
0062 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');
0063
0064 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');
0065 end
0066
0067
0068 uicontrol('Parent',currPanel,'Units','pixels','Position',[panelDimens(3)/2-10 panelDimens(4)-50-24*(y+2) 20 20],'String','+','Visible','on','Enable','on','Callback', @AddGlobal,'Style','pushbutton');
0069
0070
0071 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');
0072
0073
0074 end
0075
0076
0077
0078
0079 function ContinuousParamCheck(varargin)
0080
0081
0082
0083
0084 if isempty(bdroot) || strcmp(get_param(bdroot,'BlockDiagramType'),'library') || isempty(find_system(bdroot,'FindAll','on','Type','Annotation','Tag','ltpda model'))
0085 delete(findobj(gcf,'Parent',currPanel))
0086 currSys = gcs;
0087 gl = struct();
0088 return
0089 else
0090 if ~strcmp(gcs,currSys)
0091
0092 currSys = gcs;
0093 buildGlobalPanel();
0094 end
0095 end
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106 end
0107
0108
0109
0110
0111 function AddGlobal(varargin)
0112
0113 newGlob = 'global';
0114 while isfield(gl,newGlob), newGlob = [newGlob,'x']; end
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 newGlobName = genvarname(get(hObject,'String'));
0139 varnames = fieldnames(gl);
0140 if isfield(gl,newGlobName), set(hObject,'String',varnames{currGlob}); return; end
0141 gl = rnfield(gl,varnames{currGlob},newGlobName);
0142 updateGlobalBlock()
0143 buildGlobalPanel()
0144
0145 end
0146
0147
0148
0149 function globalValue(hObject,varargin)
0150
0151 currGlob = get(hObject,'Userdata');
0152 newValue = get(hObject,'String');
0153 varnames = fieldnames(gl);
0154 try newValue = eval(newValue); catch end
0155 gl.(varnames{currGlob}) = newValue;
0156 updateGlobalBlock()
0157 buildGlobalPanel()
0158
0159 end
0160
0161
0162
0163 function updateGlobalBlock(varargin)
0164
0165 varNames = fieldnames(gl);
0166
0167 if numel(varNames)==0
0168
0169 globBlock = find_system(bdroot,'BlockType','SubSystem','Tag','globals');
0170 if ~isempty(globBlock)
0171 delete_block(globBlock)
0172 return;
0173 end
0174 else
0175 globBlock = find_system(bdroot,'SearchDepth',1,'BlockType','SubSystem','Tag','globals');
0176 try globBlock = globBlock{1}; catch end
0177
0178 if isempty(globBlock)
0179 globBlock = add_block('ltpda_library/Commonly Used Blocks/Globals',[bdroot '/Globals']);
0180 annotation = find_system(bdroot,'FindAll','on','Type','Annotation','Tag','ltpda model');
0181 position = get_param(annotation,'Position');
0182 set_param(globBlock,'Position',[position(1)+20 position(2)-25 position(1)+150 position(2)-10 ])
0183 end
0184
0185
0186 set_param(globBlock,'UserData',gl)
0187 set_param(globBlock,'UserDataPersistent','on')
0188 end
0189
0190
0191 end
0192
0193
0194
0195 end