comparison m-toolbox/m/gui/gltpda/g_UpdateInports.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f0afece42f48
1 function g_UpdateInports(varargin)
2 % Update the MaskDisplay and the number of inports when the user
3 % changes the params from Simulink.
4
5 global params
6
7 paramKey = varargin{1};
8 if numel(paramKey)>7 && strcmpi(paramKey(1:7),'addPar_'), paramKey(1:7)=[]; end
9
10 % List of parameters coming from Simulink:
11 simKey = {};
12 for i=1:nparams(params)
13 if isa(params.params(i).val,'char') && strcmp(params.params(i).val,'-->')
14 name = params.params(i).key;
15 try if strcmpi(name(1:7),'addPar_'), name(1:7)=[]; end; catch, end
16 simKey{end+1} = name;
17 elseif isa(params.params(i).val,'cell') && isa(params.params(i).val{1},'char') && strcmp(params.params(i).val{1},'-->')
18 name = params.params(i).key;
19 try if strcmpi(name(1:7),'addPar_'), name(1:7)=[]; end; catch, end
20 simKey{end+1} = name;
21 end
22 end
23
24 % ===================================================================
25 % If varargin{2}==2 no block has been added or subtracted, just
26 % renamed an existing param; the only thing to perform is update the
27 % drawing command.
28 if varargin{2}~=2
29
30 % Remove ALL inner inport blocks.
31 if varargin{2}==-1, simKey2remove = [simKey,{paramKey}]; else simKey2remove = simKey; end
32
33 for i=1:numel(simKey2remove)
34 try %#ok<ALIGN>
35 block2delete = utils.prog.find_in_models(gcb,'SearchDepth',1,'LookUnderMasks','all','Name',lower(simKey2remove{i}));
36 if numel(block2delete)>1, block2delete(1)=[]; end
37 blockLines = get_param(block2delete{1},'LineHandles');
38 if (blockLines.Outport(1)~=-1 && ~isempty(blockLines.Outport(1))), delete_line(blockLines.Outport(1)); end
39 delete_block(block2delete);
40 catch, end
41 end
42
43 % Add ALL inner inport blocks anew:
44 for i=1:numel(simKey)
45 newBlock = add_block('simulink/Sources/In1', [gcb,'/Inport1'],'MakeNameUnique','on');
46 nameAlreadyPresent = utils.prog.find_in_models(gcb,'SearchDepth',1,'LookUnderMasks','all','Name',lower(simKey{i}));
47 if ~isempty(nameAlreadyPresent)
48 for j=1:numel(nameAlreadyPresent)
49 if ~strcmp(nameAlreadyPresent{j},gcb),
50 set_param(nameAlreadyPresent{j},'Name',[lower(simKey{i}),'_fun']);
51 end
52 end
53 end
54 set(newBlock,'Name',lower(simKey{i}))
55 end
56
57 % Now update the inner structure of lines and mux.
58 muxblock = find_system(gcb,'SearchDepth',1,'LookUnderMasks','all','BlockType','Mux');
59 try %#ok<ALIGN>
60 muxLines = get_param(muxblock{1},'LineHandles');
61 for k=1:numel(muxLines.Inport), if (muxLines.Inport(k)~=-1 && ~isempty(muxLines.Inport(k))), delete_line(muxLines.Inport(k)); end
62 end
63 for k=1:numel(muxLines.Outport), if (muxLines.Outport(k)~=-1 && ~isempty(muxLines.Outport(k))), delete_line(muxLines.Outport(k)); end
64 end
65 catch, end
66 delete_block(muxblock);
67
68 inports = find_system(gcb,'SearchDepth',1,'LookUnderMasks','all','BlockType','Inport');
69 fakeInp = find_system(gcb,'SearchDepth',1,'LookUnderMasks','all','BlockType','Ground');
70 inports = [fakeInp;inports];
71 if numel(inports)>1
72 muxblock = add_block('built-in/Mux', [gcb,'/Mux']);
73 set(muxblock,'Position',[70 , 10 , 73 , 150])
74 set(muxblock,'Inputs',num2str(numel(inports)))
75 end
76 for k=1:numel(inports)
77 inports{k}=get_param(inports{k},'Handle');
78 blkLines = get(inports{k},'LineHandles');
79 for j=1:numel(blkLines.Outport), if (blkLines.Outport(j)~=-1 && ~isempty(blkLines.Outport(j))), delete_line(blkLines.Outport(j)); end
80 end
81 set(inports{k},'Position',[10 , 10+30*(k-1) , 30 , 30+30*(k-1)])
82 set(inports{k},'Tag',num2str(k))
83 if numel(inports)>1
84 add_line(gcb,[get(inports{k},'Name'),'/1'],[get(muxblock,'Name'),'/',num2str(k)]);
85 end
86 end
87 funcBlock = find_system(gcb,'SearchDepth',1,'LookUnderMasks','all','BlockType','M-S-Function');
88 funcBlock = get_param(funcBlock,'Name');
89 if numel(inports)>1
90 add_line(gcb,[get(muxblock,'Name'),'/1'],[funcBlock{1},'/1']);
91 else
92 add_line(gcb,[get(inports{1},'Name'),'/1'],[funcBlock{1},'/1']);
93 end
94
95 end
96 % ===================================================================
97
98 % Updating the drawing command for the block Mask:
99 fakeInp = find_system(gcb,'SearchDepth',1,'LookUnderMasks','all','BlockType','Ground');
100 drawingCom = '';
101 if numel(simKey)>0 % then there are params 'from simulink' added
102 if numel(fakeInp)>0 || strcmp(get_param(gcbh,'Tag'),'arithmetic'), currInp=1; else currInp=str2double(get(findobj('Tag','numbDataInputs'),'String'))+1; end
103 for nn=1:numel(simKey)
104 drawingCom = [drawingCom,'port_label(''input'',',num2str(currInp),',''',lower(simKey{1}),''');'];
105 simKey(1) = [];
106 currInp = currInp+1;
107 end
108 else % then the only inport is the regular one
109 funcBlock = find_system(gcb,'SearchDepth',1,'LookUnderMasks','all','BlockType','M-S-Function');
110 funcName = get_param(funcBlock{1},'Tag');
111 if numel(funcName)>6 && strcmp(funcName(1:6),'ltpda_'), funcName(1:6)=[]; end
112 drawingCom = strcat('disp(''',lower(funcName),''')');
113 end
114 set(gcbh,'MaskDisplay',drawingCom)
115
116 end