Home > m > gui > gltpda > ltpdasim.m

ltpdasim

PURPOSE ^

This is the automatic function wrapper

SYNOPSIS ^

function ltpdasim(block)

DESCRIPTION ^

             This is the automatic function wrapper
 ========================================================================
 ==================== level-2 M file S-function =========================
 ========================================================================
 This wrapper is the automatic function, called by every function block
 in Simulink, able to execute m-file functions retrieving from Simulink:
 (1) the pointer to the object(s) to be analyzed, coming in as input of 
     the corresponding block (ie, the DATA),
 (2) the name of the function to be applied on those data, from the tag
     of the currently executed block (ie, the true FUNCTION),
 (3) the parameters for that particular block, retrieved from the global
     shared workspace by the handle of the block (ie, the PARAMETERS).

 The output is then generated as:
    OUTPUT = FUNCTION(DATA,PARAMETERS)

 This output in the end is saved into the global array containing all
 the AOs (ie, all the DATA go together with other data): thus this output
 will be freely accessible by all the other functions.

 The only real output to Simulink will be just the ordinal number of the
 so-generated AO into the global array of AOs.

   $Id: ltpdasim.m,v 1.2 2008/03/03 02:16:01 nicola Exp $

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function ltpdasim(block)
0002 
0003 %             This is the automatic function wrapper
0004 % ========================================================================
0005 % ==================== level-2 M file S-function =========================
0006 % ========================================================================
0007 % This wrapper is the automatic function, called by every function block
0008 % in Simulink, able to execute m-file functions retrieving from Simulink:
0009 % (1) the pointer to the object(s) to be analyzed, coming in as input of
0010 %     the corresponding block (ie, the DATA),
0011 % (2) the name of the function to be applied on those data, from the tag
0012 %     of the currently executed block (ie, the true FUNCTION),
0013 % (3) the parameters for that particular block, retrieved from the global
0014 %     shared workspace by the handle of the block (ie, the PARAMETERS).
0015 %
0016 % The output is then generated as:
0017 %    OUTPUT = FUNCTION(DATA,PARAMETERS)
0018 %
0019 % This output in the end is saved into the global array containing all
0020 % the AOs (ie, all the DATA go together with other data): thus this output
0021 % will be freely accessible by all the other functions.
0022 %
0023 % The only real output to Simulink will be just the ordinal number of the
0024 % so-generated AO into the global array of AOs.
0025 %
0026 %   $Id: ltpdasim.m,v 1.2 2008/03/03 02:16:01 nicola Exp $
0027 
0028 
0029 setup(block);
0030 
0031 
0032 %% Setup
0033 
0034 function setup(block)
0035 
0036   %% Register dialog parameter: none, because they're retrieved directly
0037   %% from the memory. This will prevent the user to modify the parameters
0038   %% outside the proper parameters panel:
0039   block.NumDialogPrms = 0;
0040 
0041   %% Register number of input and output ports
0042   block.NumInputPorts  = 1;
0043   block.NumOutputPorts = 1;
0044 
0045   %% Setup functional port properties to dynamically inherited.
0046   block.SetPreCompInpPortInfoToDynamic;
0047 % block.SetPreCompOutPortInfoToDynamic;
0048 
0049   block.InputPort(1).DirectFeedthrough = true;
0050   block.InputPort(1).DatatypeID = 0;
0051   block.InputPort(1).Complexity = 0;
0052   block.OutputPort(1).DatatypeID = 0;
0053   block.OutputPort(1).Complexity = 0;
0054   block.SampleTimes = [0 0];
0055   block.SetAccelRunOnTLC(false);
0056   
0057   block.OutputPort(1).Dimensions = 1;
0058   
0059   %% Register methods
0060   block.RegBlockMethod('SetInputPortSamplingMode',@SetInpPortFrameData);
0061   block.RegBlockMethod('SetInputPortDimensions',  @SetInpPortDims);
0062 % block.RegBlockMethod('SetOutputPortDimensions', @SetOutPortDims);
0063   block.RegBlockMethod('Outputs',                 @Outputs);
0064   
0065   function SetInpPortFrameData(block, idx, fd)
0066   block.InputPort(1).SamplingMode = fd;
0067   block.OutputPort(1).SamplingMode  = fd;
0068   
0069   function SetInpPortDims(block, idx, di)
0070   block.InputPort(idx).Dimensions = di;
0071   
0072 %   function SetOutPortDims(block, idx, di)
0073 % % %       if isempty(block.DialogPrm(1).Data)
0074 % % %           numbOuts = 1;
0075 % % %       else
0076 % % %           numbOuts = block.DialogPrm(1).Data;
0077 % % %       end
0078 % % %       if isempty(find_system(get_param(gcbh,'Parent'),'SearchDepth',1,'LookUnderMasks','all','BlockType','Mux'))  && ~exist('category','var')
0079 % % % %           block.OutputPort(1).Dimensions = di^(numbOuts);
0080 % % %           outdims=(block.InputPort(1).Dimensions)^numbOuts
0081 % % %           block.OutputPort(idx).Dimensions = outdims;
0082 % % %
0083 % % %       end
0084 
0085 
0086 
0087 %% Outputs
0088 function Outputs(block)
0089 global LTPDAinvar
0090 
0091 % Check if the user wants to stop the execution:
0092 lastChar = get(gcf,'CurrentCharacter');
0093 if ~isempty(lastChar) && strcmp(lastChar,'x')
0094     set_param(bdroot, 'SimulationCommand', 'stop')
0095     return
0096 end
0097 
0098 % Update the progress bar window:
0099   blocksTotal = get(findobj('Tag','blockstotal'),'UserData');
0100   blocksDone  = get(findobj('Tag','done'),'UserData');
0101   blocksToGo  = get(findobj('Tag','togo'),'UserData');
0102   set(findobj('Tag','done'),'UserData',blocksDone+1);
0103   set(findobj('Tag','togo'),'UserData',blocksToGo-1);
0104   set(findobj('Tag','done'),'String',['Done: ',num2str(blocksDone+1)]);
0105   set(findobj('Tag','togo'),'String',['To go: ',num2str(blocksToGo-1)]);
0106   set(findobj('Tag','currentexec'),'String',['Currently executed block: ',get_param(get_param(gcbh,'Parent'),'Name')]);
0107   progressBar  = findobj('Tag','progressaxes');
0108   progressSize = get(progressBar,'UserData');
0109   progressPosition    = get(progressBar,'Position');
0110   progressPosition(3) = 1+(progressSize(3)/blocksTotal)*(blocksDone+1);
0111   set(progressBar,'Position',progressPosition)
0112   pause(0.1)
0113 
0114 % Retrieve the function name from the block tag:
0115   blocktag = get_param(gcb, 'tag');
0116   
0117 % Convert it into lowercase letters:
0118   blocktag = lower(blocktag);
0119   
0120 %%
0121 % ========================================================================
0122 % ==================== RETRIEVE THE PARAMETERS LIST ======================
0123 % ========================================================================
0124 
0125   % retrieve the handle of the currently executed block;
0126     currparent = get_param(gcbh,'Parent');
0127     subsyshandle=get_param(currparent, 'handle');
0128 
0129   % The command line to produce the proper parameters list for the block
0130   % currently executed is retrieved from the description of this  parent subsystem
0131     paramcommand = get_param(subsyshandle,'Description');
0132 
0133     if ~isempty(paramcommand)
0134         eval(paramcommand);
0135         if ~exist('paramEnabled','var'), paramEnabled = ones(1,nparams(params)); end
0136         currparam = plist();
0137         for jj=1:nparams(params)
0138             if paramEnabled(jj)==1
0139                 try %#ok<ALIGN>
0140                 if strcmpi(params.params(jj).key(1:7),'addPar_'), params.params(jj).key(1:7)=[]; end
0141                 catch end
0142 %                 try % for cell array params:
0143 %                     if isa(params.params(jj).val,'char') && strcmp(params.params(jj).val(1),'{'), params.params(jj).val = eval(params.params(jj).val) ; end
0144 %                 catch
0145 %                 end
0146                 currparam = append(currparam,params.params(jj));
0147             end
0148         end
0149     else
0150         currparam = plist();
0151     end
0152 
0153   % Retrieve parameters from Simulink:
0154     paramFromSimulink = 0;
0155     for i=1:nparams(currparam)
0156          paramKey = get(currparam.params(i),'key');
0157          paramVal = get(currparam.params(i),'val');
0158          if (isa(paramVal,'char') && strcmp(paramVal,'-->')) || (isa(paramVal,'cell') && numel(paramVal)==1 && isa(paramVal{1},'char') && strcmp(paramVal{1},'-->'))
0159              paramFromSimulink = paramFromSimulink + 1;
0160              mux = find_system(currparent,'SearchDepth',1,'LookUnderMasks','all','BlockType','Mux');
0161              muxWidths = get_param(mux{1},'CompiledPortWidths');
0162              muxWidths = muxWidths.Inport;
0163              inputBlock = find_system(currparent,'SearchDepth',1,'LookUnderMasks','all','Name',lower(paramKey));
0164              inputNumb  = str2double(get_param(inputBlock{1},'Tag'));
0165              x = 0;
0166              for j=1:inputNumb-1
0167                  x=x+muxWidths(j);
0168              end
0169              paramVal = [LTPDAinvar{block.InputPort(1).Data(x+1)}];
0170              if muxWidths(inputNumb)>1
0171                  for j=x+2:x+muxWidths(inputNumb)
0172                      paramVal = [paramVal,LTPDAinvar{block.InputPort(1).Data(j)}];
0173                  end
0174              end
0175              currparam  = pset(currparam,paramKey,paramVal);
0176          end
0177     end
0178     
0179 %%
0180 % ========================================================================
0181 % ======================== CALCULATE THE RESULTS =========================
0182 % ========================================================================
0183 
0184    y = length(block.InputPort(1).Data) - paramFromSimulink;
0185 
0186  % Check to verify the type of function:
0187    specFunc = {'display'};
0188    [func,classes] = strtok(get(get_param(currparent,'Handle'),'Tag'));
0189    if strcmp(func,'method')
0190        category = eval(sprintf([blocktag,'(%s,''Category'')'],strtrim(classes)));
0191    end
0192    
0193    if ismember(blocktag,specFunc)
0194      % Input, but no parameters and no output
0195        type = 4;
0196      % ======================================
0197    elseif (exist('category','var') && strcmp(category,'Output'))
0198      % Input and parameters, but no output
0199        type = 3;
0200      % ===================================
0201    elseif ~isempty(find_system(currparent,'SearchDepth',1,'LookUnderMasks','all','BlockType','Constant'))
0202      % No input, but parameters and output
0203        type = 2;
0204      % ===================================
0205    else
0206      % Input, parameters and output
0207        type = 1;
0208      % ============================
0209    end
0210 
0211 
0212    switch type
0213      % ====================================================================
0214        case 4 % Input, but no parameters and no output
0215            inputlist = 'LTPDAinvar{block.InputPort(1).Data(1)}';
0216            for i=2:y
0217                inputlist = [inputlist,',LTPDAinvar{block.InputPort(1).Data(',num2str(i),')}'];
0218            end
0219            command = ['feval(blocktag,',inputlist,')'];
0220            block.OutputPort(1).Data = block.InputPort(1).Data;
0221            try
0222                eval(command)
0223            catch
0224                disp('4')
0225                disp(sprintf('Something''s wrong in the calculation of the results of the %s function.',blocktag))
0226                disp(sprintf('The name of the currently executed block is %s.',get_param(currparent,'Name')))
0227                disp(sprintf('The (first) input data ordinal number, in the list of objects, was %d.',block.InputPort(1).Data(1)))
0228                rethrow(lasterror)
0229            end
0230 %          block.OutputPort(1).Data = block.InputPort(1).Data;
0231            block.OutputPort(1).Data = 1;
0232            return;
0233 
0234      % ====================================================================
0235        case 3 % Input and parameters, but no output
0236            try
0237                inputlist = 'LTPDAinvar{block.InputPort(1).Data(1)}';
0238                for i=2:y
0239                    inputlist = [inputlist,',LTPDAinvar{block.InputPort(1).Data(',num2str(i),')}'];
0240                end
0241                command = ['feval(blocktag,',inputlist,',currparam);'];
0242                eval(command);
0243            catch
0244                disp('3')
0245                disp(sprintf('Something''s wrong in the calculation of the results of the %s function.',blocktag))
0246                disp(sprintf('The name of the currently executed block is %s.',get_param(currparent,'Name')))
0247                disp(sprintf('The (first) input data ordinal number, in the list of objects, was %d.',block.InputPort(1).Data(1)))
0248                disp('The plist passed was:')
0249                currparam
0250                rethrow(lasterror)
0251            end
0252 %          block.OutputPort(1).Data = block.InputPort(1).Data;
0253            block.OutputPort(1).Data = 1;
0254            return;
0255 
0256      % ====================================================================
0257        case 2 % No input, but parameters and output
0258            try
0259                outdata = feval(blocktag,currparam);
0260            catch
0261                disp('2')
0262                disp(sprintf('Something''s wrong in the calculation of the results of the %s function.',blocktag))
0263                disp(sprintf('The name of the currently executed block is %s.',get_param(currparent,'Name')))
0264                disp('The plist passed was:')
0265                currparam
0266                rethrow(lasterror)
0267            end
0268 
0269      % ====================================================================
0270        case 1 % Input, parameters and output
0271            try
0272                inputlist = 'LTPDAinvar{block.InputPort(1).Data(1)}';
0273                for i=2:y
0274                    inputlist = [inputlist,',LTPDAinvar{block.InputPort(1).Data(',num2str(i),')}'];
0275                end
0276                command = ['outdata = feval(blocktag,',inputlist,',currparam);'];
0277                eval(command);
0278            catch
0279                disp('1')
0280                disp(sprintf('Something''s wrong in the calculation of the results of the %s function.',blocktag))
0281                disp(sprintf('The name of the currently executed block is %s.',get_param(currparent,'Name')))
0282                disp(sprintf('The (first) input data ordinal number, in the list of objects, was %d.',block.InputPort(1).Data(1)))
0283                disp('The plist passed was:')
0284                currparam
0285                rethrow(lasterror)
0286            end
0287 
0288    end
0289 
0290 %%
0291 
0292 % ========================================================================
0293 % =============================== OUTPUTS ================================
0294 % ========================================================================
0295 
0296 signalName = get_param(currparent,'Name');
0297 if ~isempty(get_param(currparent,'UserData'))
0298     if numel(outdata)==1
0299         outdata = setnh(outdata,'name',signalName);
0300     else
0301         for i=1:size(outdata,1)
0302             for j=1:size(outdata,2)
0303                 outdata(i,j) = setnh(outdata(i,j),'name',[signalName,'_',num2str(i),'-',num2str(j)]);
0304             end
0305         end
0306     end
0307 end
0308         
0309     try
0310         
0311 % % %       % To put multiple elements into multiple lines of LTPDAinvar:
0312 % % %         oldx = size(LTPDAinvar,1);
0313 % % %         for i = 1:numel(outdata)
0314 % % %             LTPDAinvar = [LTPDAinvar;{outdata(i),0}];
0315 % % %         end
0316 % % %         x = size(LTPDAinvar,1);
0317 % % %         results = oldx+1:x;
0318 % % %         block.OutputPort(1).Data = results;
0319 
0320       % To put multiple elements into a single line of LTPDAinvar:
0321         x = size(LTPDAinvar,1);
0322         LTPDAinvar = [LTPDAinvar;{outdata,0}];
0323         block.OutputPort(1).Data = x+1;
0324         
0325       % To prevent the result calculated here from being canceled at the
0326       % end of the calculation:
0327         probe = get_param(currparent,'UserData');
0328         if ~isempty(probe) && isa(probe,'char') && strcmp(probe,'probe')
0329             LTPDAinvar{size(LTPDAinvar,1),2} = 2;
0330         end
0331 
0332     catch
0333         disp('------------------============================================------------------')
0334         disp(sprintf('Something''s wrong in appending to the global variable LTPDAinvar the results of the %s function.',blocktag))
0335         disp(sprintf('The name of the currently executed block is %s.',get_param(currparent,'Name')))
0336         disp('The data to be saved, calculated into this block, are:')
0337         outdata
0338         size(outdata)
0339         disp('------------------============================================------------------')
0340     end
0341 
0342     % the output to Simulink is just the ordinal number (ONE AND ONLY ONE
0343     % NUMBER NOW!) of the AOs appended to the global array LTPDAinvar.
0344 
0345 % endfunction

Generated on Fri 07-Mar-2008 15:46:43 by m2html © 2003