select bus elements programmatically

Illustration
Rutherford - 2021-10-07T12:48:43+00:00
Question: select bus elements programmatically

I have signals in a SIMULINK bus and I would like to select specific signals from it. The issue is that I want to run an initialization script that will decide which subset of signals to select. I see that you can select bus signals manually via the bus selector block. Is there any way to perform this function programmatically?  

Expert Answer

Profile picture of John Michell John Michell answered . 2025-12-15

Use set_param on the 'OutputSignals' Parameter

The key parameter for a Bus Selector block is OutputSignals, which specifies the selected elements as a comma-separated string of element names (hierarchical names for nested buses, e.g., 'bus1.signalA,bus2.nestedSignal').

Example Code

Assume your model is loaded, and the Bus Selector block path is 'myModel/mySubsystem/BusSelector1'.

  1. Get current selected signals (optional, to build upon existing):

    matlab
     
    currentSignals = get_param('myModel/mySubsystem/BusSelector1', 'OutputSignals');
    disp(currentSignals);  % e.g., 'signal1,signal2,nested.busSignal'
     
     
  2. Set new selected signals (overwrites all; specify the full list):

    matlab
     
    set_param('myModel/mySubsystem/BusSelector1', 'OutputSignals', 'signalA,signalB,nestedBus.signalX');
     
     
    • This updates the block: adds/removes output ports accordingly.
    • Element names must match exactly what's available in the input bus (case-sensitive, including hierarchy like 'topBus.subBus.element').
    • For a single element: 'mySignal'
    • For none (clear all): '' (empty string)
  3. To add to existing without overwriting:

    matlab
     
    current = get_param('myModel/mySubsystem/BusSelector1', 'OutputSignals');
    newList = [current ',newSignal'];  % Append (add comma if current not empty)
    if ~isempty(current)
        newList = [current ',newSignal'];
    else
        newList = 'newSignal';
    end
    set_param('myModel/mySubsystem/BusSelector1', 'OutputSignals', newList);
     
     

Additional Options

  • Output as virtual bus: If you want selected elements grouped into one virtual bus output port:
    matlab
     
    set_param('myModel/mySubsystem/BusSelector1', 'OutputAsBus', 'on');  % or 'off' for separate ports
     
     
  • After setting, update the model diagram if needed:
    matlab
     
    set_param('myModel', 'SimulationCommand', 'update');


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!