【问题标题】:Discover if a Simulink signal is a bus programmatically以编程方式发现 Simulink 信号是否为总线
【发布时间】:2018-01-14 03:43:28
【问题描述】:

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

我可以处理信号的查找/获取句柄、添加块、设置变量名等等。然而,我想做的是以编程方式发现所选信号是标量、数组还是总线信号。这样,我可以将数组重塑为一维数组(以便它们以我想要的方式连接)并将总线信号分解为它们的单个元素。

我尝试了在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 );

【问题讨论】:

  • 为了帮助其他人回答您的问题,请在问题中发布您的格式化代码。

标签: matlab command signals simulink bus


【解决方案1】:

我想我找到了解决办法。

在使用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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-09
    • 2016-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多