Simulink: How to set TestPoint state of all selected signals programmatically

The following code snippet demonstratests how to set the TestPoint state of all currently selected signal lines to 1 (i.e. testpoint enabled) in a Simulink model using MATLAB.

It builds upon our previous post Simulink: How to query selected signal / line programatically.

% Find all selected lines
lineHandles = find_system(gcs, 'FindAll', 'on', 'Type', 'line', 'Selected', 'on');

% Check if any lines are selected
if isempty(lineHandles)
    disp('No lines are currently selected.');
else
    % Loop through all selected lines and set them as testpoints
    for i = 1:length(lineHandles)
        lineHandle = lineHandles(i);
        
        % Set the line as a testpoint
        set(lineHandle, 'TestPoint', 1);
        
        % Optional: Get signal name for confirmation
        signalName = get(lineHandle, 'Name');
        if isempty(signalName)
            fprintf('Line %d set as testpoint\n', i);
        else
            fprintf('Signal "%s" set as testpoint\n', signalName);
        end
    end
end

mktestpoint.m

The above code can be saved as a MATLAB script file named mktestpoint.m for easy reuse:

% mktestpoint.m
% This script sets the TestPoint state of all currently selected signal lines to a given value (1 for enabled, 0 for disabled).
% Usage: Select signal lines in your Simulink model and run this script.
```matlab
function mktestpoint(enable)
  % mktestpoint.m
  % This function sets the TestPoint state of all currently selected signal lines to the given value (1 for enabled, 0 for disabled).
  % Usage: Select signal lines in your Simulink model and call mktestpoint() to enable (default) or mktestpoint(0) to disable.
  
  if nargin < 1
      enable = 1;
  end
  
  % Find all selected lines
  lineHandles = find_system(gcs, 'FindAll', 'on', 'Type', 'line', 'Selected', 'on');

  % Check if any lines are selected
  if isempty(lineHandles)
    disp('No lines are currently selected.');
  else
    % Loop through all selected lines and set them as testpoints
    for i = 1:length(lineHandles)
      lineHandle = lineHandles(i);
      
      % Set the line as a testpoint
      set(lineHandle, 'TestPoint', enable);
      
      % Optional: Get signal name for confirmation
      signalName = get(lineHandle, 'Name');
      if isempty(signalName)
        fprintf('Line %d set as testpoint (%d)\n', i, enable);
      else
        fprintf('Signal "%s" set as testpoint (%d)\n', signalName, enable);
      end
    end
  end
end