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.

CreateTimeSeries(Data,tStart,Ts,varargin)
function Obj = CreateTimeSeries(Data,tStart,Ts,varargin)
% CreateTimeSeries - Create a Simulink.TimeSeries object
% ----------------------------------------------------------------------
% This function creates a Simulink.TimeSeries object, useful as part of
% creating a Simulink.TsArray.  For more info, see help for CreateTsArray.
%
% Syntax:
%       Obj = CreateTimeSeries(Data)
%       Obj = CreateTimeSeries(Data,tStart)
%       Obj = CreateTimeSeries(Data,tStart,Ts)
%       Obj = CreateTimeSeries(Data,tStart,Ts,'property1','value1',...)
%
% Inputs:
%       Data - Array or matrix of input data
%       tStart - Start time of the time vector
%       Ts - Sample time of the time vector
%
% Examples:
%
%       Data = uint8(255*rand(101,1));
%       Obj = CreateTimeSeries(Data, 0, 0.1, 'SignalName', 'MySignal')
%       Obj.SignalName = 'NewSignalName'
%
% Notes:
%   - This is **UNDOCUMENTED** functionality that is subject to change in a
%     future MathWorks release.
%   - To pass inputs of a specific data type, you must first convert the
%     data array to that data type.  For example:
%        Data = uint8(Data);
%   - Only built-in MATLAB/Simulink data types are supported
%   - Additional properties can be set by using property value pairs.  The
%     common properties to set are: 'Name', 'BlockPath', 'PortIndex',
%     'SignalName', 'ParentName'.  Note that not all of these fields must
%     be filled in to use the Simulink.TimeSeries object as an input.
%   - Changing Data and Time vectors after creating the Simulink.TimeSeries
%     object is not supported.
%   - Matrix signals (m*n*o) are not supported
%
% Copyright 2006 - 2010 The MathWorks, Inc.


%% Check Input Arguments

% Data must be given
if nargin < 1
    error('Data vector must be specified.');
end

% Default Start Time to 0
if nargin < 2
    tStart = 0;
end

% Default Sample Time to 1
if nargin < 3
    Ts = 1;
end

% Ensure size of varargin is divisible by 2
if rem(numel(varargin),2)
    error('Incorrect specification of input property/value pairs.');
end


%% Create the TimeSeries Object

% Determine End Time (tEnd)
% For multi-D data, time series must be last index in vector
if ndims(Data) > 2
    tEnd = tStart + Ts * (size(Data,ndims(Data))-1);
else    
    tEnd = tStart + Ts * (size(Data,1)-1);
end

% Create a vector [0,0] with the correct datatype.  This is needed for
% initializing the object.
DataType = class(Data);

% Initialize the object
Obj = Simulink.Timeseries;
Obj.initialize('','','','','',Data,[],tStart,Ts,tEnd,[],[0,0]);

% Set any varargin properties
ObjFields = fields(Obj);
for i=1:2:numel(varargin)
    if ismember(varargin{i},ObjFields)
        set(Obj,varargin{i},varargin{i+1});
    else
        error(['''' varargin{i} ''' is not a valid Simulink.TimeSeries property.']);
    end
end

Contact us