Main Content

getTimeValues

Class: io.reader
Namespace: io

Return time values for data imported using a custom reader

Since R2020b

Syntax

timeVals = getTimeValues(obj)

Description

timeVals = getTimeValues(obj) returns time values to use for data imported into the Simulation Data Inspector using a custom file or workspace variable reader. Specify code for the getTimeValues method to extract the data from a proprietary file or variable format.

Input Arguments

expand all

Custom data reader, specified as an object of a class that inherits from the io.reader base class.

Example: MyCustomFileReader

Output Arguments

expand all

Time values for imported data, returned as a column vector.

Attributes

Abstracttrue

To learn about attributes of methods, see Method Attributes.

Examples

expand all

Write the function definition for the getTimeValues method to return time values to use for data imported from a file. The custom reader in this example always uses the data in the first column of the file as time. Specify the code for the getTimeValues method in the class definition file.

This example does not show a complete class definition. All custom readers must define behavior for the getName, getTimeValues, and getDataValues methods. For an example that shows the complete class definition and import workflow, see Import Data Using Custom File Reader.

In this example, the getTimeValues method reads the data from the file using the readtable function and returns the data from the first column to use as the time values for imported signals.

classdef ExcelFirstColumnTimeReader < io.reader
  methods
    % ...

    function timeVals = getTimeValues(obj)
      timeVals = [];
      if ~isempty(obj.VaribleName)
        t = readtable(obj.FileName);
        timeName = t.Properties.VariableNames{1};
        timeVals = t.(timeName);
      end  
    end

  % ...
  end
end

Write the function definition for the getTimeValues method to return time values for data imported from the workspace. Specify the code executed by the getTimeValues method in the class definition file.

This example does not show a complete class definition. All custom readers must define behavior for the getName, getTimeValues, and getDataValues methods, and workspace data readers need to define the supportsVariable method. For an example that shows the complete class definition and import workflow for a workspace data reader, see Import Workspace Variables Using a Custom Data Reader.

The custom reader in this example imports a structure or an array of structures from the workspace. The structures must contain fields for the signal data (d), the time data (t), and the signal name (n). When the variable to import is a scalar structure, the getTimeValues method returns the value in the t field of the imported structure.

When the variable is an array of structures, the custom reader uses both the getTimeValues and getChildren methods to return time data. The getChildren method creates a custom reader object for each structure in the array and sets the ChannelIndex property to identify the index of the signal data within the array. Then, the getTimeData method uses the ChannelIndex property value to select the appropriate structure from the VariableValue property value, which is the array of structures.

classdef ExcelFirstColumnTimeReader < io.reader
  
  properties
    ChannelIndex
  end

  methods
    % ...

    function childObj = getChildren(obj)
      childObj = {};
      if ~isscalar(obj.VariableValue) && isempty(obj.ChannelIndex)
        numChannels = numel(obj.VariableValue);
        childObj = cell(numChannels,1);
        for idx = 1:numChannels
          childObj{idx} = SimpleStructReader;
          childObj{idx}.VariableName = sprintf('%s(%d)',obj.VariableName,idx);
          childObj{idx}.VariableValue = obj.VariableValue;
          childObj{idx}.ChannelIndex = idx;
        end
      end  
    end

    function timeVals = getTimeValues(obj)
      if isscalar(obj.VariableValue)
        timeVals = obj.VariableValue.t;
      elseif ~isempty(obj.ChannelIndex)
        varVal = obj.VariableValue(obj.ChannelIndex);
        timeVals = varVal.t;
      else
        timeVals = [];
      end  
    end

  % ...
  end
end

Version History

Introduced in R2020b