Main Content

getChildren

Class: io.reader
Package: io

Return custom reader object for each node and signal in data imported using a custom reader

Syntax

childObj = getChildren(obj)

Description

childObj = getChildren(obj) returns an array of custom reader objects that correspond to hierarchical nodes and signals in data imported into the Simulation Data Inspector using a custom file or workspace reader. Use the getChildren method to preserve the hierarchy for imported data. When you import multiple signals from a file, you can use the getChildren method to import the data as a flat list of signals contained under a top-level node that corresponds to the file.

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

Hierarchical nodes and signals in imported data, returned as a cell array of io.reader subclass objects.

Examples

expand all

This example uses the getChildren method to import multiple signals from a file as a flat list of signals contained in a top-level node that represents the file. Specify code for the getChildren 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 a Custom File Reader.

In this example, the getChildren method reads the data from the file using the readtable function. The custom reader in this example always uses the data in the first column as time, so the first variable name is cleared and not used. The getChildren method creates a custom reader object for the top-level node that corresponds to the file and for each signal in the file and assigns values to the FileName and VariableName properties.

classdef ExcelFirstColumnTimeReader < io.reader
  methods
    % ...

    function childObj = getChildren(obj)
      childObj = {};
      if isempty(obj.VariableName)
        t = readtable(obj.FileName);
        vars = t.Properties.VariableNames;
        vars(1) = [];
        childObj = cell(size(vars));
        for idx = 1:numel(vars)
            childObj{idx} = ExcelFirstColumnTimeReader;
            childObj{idx}.FileName = obj.FileName;
            childObj{idx}.VariableName = vars{idx};
        end
      end
    end
    
  % ...
  end
end 

This example uses the getChildren method to import an array of structures stored in a variable in the base workspace. Specify code for the getChildren 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.

In this example, the getChildren method creates a custom reader object for each structure in the input variable when the variable is an array of structures. The workspace reader in this example defines a ChannelIndex property that the getChildren method uses to identify each object it creates.

classdef SimpleStructReader < 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
    
  % ...
  end
end 
Introduced in R2020b