Code covered by the BSD License  

Highlights from
Example: Creating TimeSeries and TsArray Inputs for Simulink Bus Signal Input Data

image thumbnail

Example: Creating TimeSeries and TsArray Inputs for Simulink Bus Signal Input Data

by

 

06 Aug 2007 (Updated )

Create timeseries inputs for Simulink model root inports that are bus signals.

CreateTsArray(varargin)
function Obj = CreateTsArray(varargin)
% CreateTsArray - Create a Simulink.TsArray object
% ----------------------------------------------------------------------
% This function creates a Simulink.TsArray object, which is useful for
% simulating a model that accepts a bus object at an input port.  The
% TsArray object may be comprised of Simulink.TimeSeries and/or additional
% TsArray objects.  
%
% Syntax:
%       Obj = CreateTsArray(ts1,ts2,...)
%
% Inputs:
%       ts1, ts2,... = Simulink.TsArray or Simulink.TimeSeries to be added
%                      as child objects
%
% Examples:
%
%       Data1 = uint8(255*rand(101,1));
%       Data2 = uint16(255*rand(101,2));
%       ts1 = CreateTimeSeries(Data, 0, 0.1, 'SignalName', 'MySignal1')
%       ts2 = CreateTimeSeries(Data, 0, 0.1, 'SignalName', 'MySignal2')
%       InputData = CreateTsArray(ts1,ts2)
%
% Notes:
%   - This is **UNDOCUMENTED** functionality that is subject to change in a
%     future MathWorks release.
%   - Inputs ts1, ts2, etc. can be another TsArray.  This is useful for
%     nested bus objects
%
% Copyright 2006 - 2009 The MathWorks, Inc.


% Create a new TsArray object (Obj)
Obj = Simulink.TsArray;

% Loop through each element being added to the TsArray
for i=1:numel(varargin)
    
    % Determine the class and size of this element
    switch class(varargin{i})
        case 'Simulink.TsArray'
            ClassTypes{i} = 'TsArray'; %#ok<*AGROW>
            NumElems{i} = numel(varargin{i}.Members);
        case 'Simulink.Timeseries'
            ClassTypes{i} = 'Timeseries';
            NumElems{i} = 1;
        otherwise
            error('Unrecognized input argument.  Only Simulink.TsArray and Simulink.TimeSeries are supported inputs.');
    end
    
    % Pick a name for this element    
    if ~isempty(varargin{i}.Name)
        ElementNames{i} = varargin{i}.Name;
    else
        ElementNames{i} = [ClassTypes{i} int2str(i)];
    end

    % Add this element as a property of the new TsArray object (Obj)
    schema.prop(Obj,ElementNames{i},'MATLAB array');

    % Set the new element
    Obj.(ElementNames{i}) = varargin{i};

end

% Define the members of the new TsArray object (Obj)
Obj.Members = struct(...
    'name',  ElementNames,...
    'elems', NumElems,...
    'class', ClassTypes );

% Specify that this is a Simulink bus
set(Obj,'isBus',true);

Contact us