通过编程方式发现Simulink信号是否为总线

时间:2018-01-14 03:43:29

标签: matlab command signals simulink bus

我正在使用MATLAB 2013b,我正在尝试编写一个脚本,它会自动将ToWorkspace块连接到一组选定的信号。

我可以处理查找/获取信号的句柄,添加块,设置变量名等等。但是,我想要的是以编程方式发现所选信号是标量,数组还是总线信号。这样,我可以将数组重新整形为1D(以便它们以我想要的方式连接)并将总线信号分解为各自的元素。

我尝试在See if a signal originates from a bus in Simulink发布修复但没有运气。我打赌该帖子已经足够大,以至于提议的修复程序在我试图使用的MATLAB版本中不起作用。

我知道答案涉及编译模型或使用特定命令更新模型,但我被困住了。求救!

以下是我尝试做的事情,基于上面的链接:

function usedNames = addToWorkspaceBlock( signal, usedNames )
   % Recursively add whatever blocks are necessary to break down the signal
   % into raw bus elements or reshaped arrays and output them that way

   switch upper( get(signal,'CompiledBusType') )
      case 'NOT_BUS'
         % Will add the reshape block and ToWorkspace block here, saving
         % the name of the ToWorkspace VariableName to usedNames
         keyboard % for debugging
      case {'VIRTUAL_BUS','NON_VIRTUAL_BUS'}
         % Will add the reshape block and ToWorkspace block here, saving
         % the name of the ToWorkspace VariableName to usedNames
         keyboard % for debugging
      otherwise
         error('Unrecognized CompiledBusType %s', get(signal,'CompiledBusType'));
   end
end

我得到的错误是:(忽略行号;我的addToWorkspaceBlock()函数是我的主函数的子函数,它处理获取信号并循环通过它们)

Error using createToWorkspaceBlocks>addToWorkspaceBlock (line 48)
    line does not have a parameter named 'CompiledBusType'

Error in createToWorkspaceBlocks (line 36)
    usedNames = addToWorkspaceBlock( signals(i), usedNames );

1 个答案:

答案 0 :(得分:0)

我想我找到了解决方案。

使用modelName([],[],[],'compile')编译模型并使用modelName([],[],[],'sizes')执行了大小阶段后,CompiledPortDimensions现在将成为信号源端口的属性。如果信号是总线,则第一个CompiledPortDimensions将为负。如果它是数字(标量或数组),CompiledPortDimensions将显示信号的size()

我已在下面更新了我的代码。请注意,我没有显示我对信号的实际发现或我将如何添加块,但这就是我最终的结果:

function createToWorkspaceBlocks(signals)

   % Create a ToWorkspace output for each of the given signals


   % Get the root model containing these signals (assume all given signals
   % are in the same root diagram)
   rootSystem = bdroot( get(signals(1),'Parent') );


   % Update the model to establish the CompiledPortDimensions property and rethrow
   % any errors that occur during (i.e., stop this function)
   try
      cmd = [ rootSystem '([],[],[],''compile'')' ];
      evalin('base',cmd);
      %       set_param( rootSystem, 'SimulationCommand', 'update' );
   catch me
      warndlg('The model could not be compiled!');
      rethrow(me);
   end

   cmd = [ rootSystem '([],[],[],''sizes'')' ];
   evalin('base',cmd);


   % Loop through the signals and determine what signal type it is. Make
   % ToWorkspace blocks based on the signal type:
   %  - scalars: signal --> ToWorkspace block
   %  - arrays:  signal --> Reshape(1D) --> ToWorkspace block
   %  - bus:     signal --> BusSelector --> (Reshape(1D) if needed) --> ToWorkspaceBlock
   % Recursively
   usedNames = cell( numel(signals), 1 );

   for i = 1 : numel(signals)
      usedNames = addToWorkspaceBlock( signals(i), usedNames );
   end

   cmd = [ rootSystem '([],[],[],''term'')' ];
   evalin('base',cmd);

end

function usedNames = addToWorkspaceBlock( signal, usedNames )
   % Recursively add whatever blocks is necessary to break down the signal
   % into raw bus elements or reshaped arrays and output them that way

   srcPort     = get( signal, 'SrcPortHandle' );
   srcPortDims = get( srcPort, 'CompiledPortDimensions' );

   if srcPortDims(1) < 0
      % Signal is a BUS

   elseif all(srcPortDims > 0)
      % Signal is NUMERIC
      % % Add reshape block
      % % Add ToWorkspace block
   end
end