No BSD License  

Highlights from
Log Simulink Signals

image thumbnail
from Log Simulink Signals by Cole Stephens
Log specific signals in a Simulink model via the MATLAB command line.

Log_Signals(varargin)
function [] = Log_Signals(varargin)
%% Log_Signals
% This function turns signal logging on for user-specified signal names.
% 
%Log_Signals('all')
%   Will log all named signals in the diagram.
%Log_Signals('none')
%   Will not log any named signals in the diagram.
%Log_Signals('x1','x2',...)
%   Will log signals, x1, x2, etc provided a corresponding signal name
%   exists in the diagram.

%% Check to see if a block diagram is open
    if isempty(gcs)
        disp('There are no Simulink diagrams open.');
        return;
    end
    
%%  Get a list of all the blocks in the current system.
Blocks = find_system(gcs, 'regexp', 'on', 'blocktype','.*');
hdlBlocks = get_param(Blocks, 'handle');

%% Get a list of all the output ports.
% Initialize a counter...will keep track of the number of named signals in
% the block diagram.
cntr=0;
% Loop through all the blocks store the ones that have a named port handle.
for idx = 1:length(hdlBlocks)
    % testpointing is tied to ports...so find all the ports, specifically
    % the output ports.
    dummy = get_param(hdlBlocks{idx},'PortHandles');
    if ~isempty(dummy.Outport)
        % You can only log a signal with a name...
        for jdx=1:length(dummy.Outport)
            if ~isempty(get(dummy.Outport(jdx),'name'))
                cntr=cntr+1;
                hdlOutputPorts{cntr} = dummy.Outport(jdx);
            end
        end
    end
end

if ((nargin == 1) && (strcmpi(varargin{1},'all')))
    for idx=1:length(hdlOutputPorts)
        set_param(hdlOutputPorts{idx},'testpoint','on'...
                                     ,'datalogging','on');
    end
elseif ((nargin == 1) && (strcmpi(varargin{1},'none')))
    for idx=1:length(hdlOutputPorts)
        set_param(hdlOutputPorts{idx},'datalogging','off'...);
                                     ,'testpoint','off')
    end
elseif nargin >= 1
    % Get the names on the output ports
    Names{length(hdlOutputPorts)} = '';
    for idx=1:length(hdlOutputPorts)
        Names{idx}=get(hdlOutputPorts{idx},'name');
    end
    
    for idx=1:length(varargin)
        temp=strcmp(Names,varargin{idx});
        if ~isempty(find(temp,1))
            set_param(hdlOutputPorts{find(temp==1)}...
                ,'testpoint','on', 'datalogging','on')
        else
            disp(['''',varargin{idx},'''',' not found.'])
        end
    end
     
else
    disp('You must provide at least 1 function argument');
end

Contact us at files@mathworks.com