Main Content

supportsVariable

Class: io.reader
Namespace: io

Return logical indication of whether custom reader supports variable

Since R2020b

Syntax

supported = supportsVariable(obj,var)

Description

supported = supportsVariable(obj,var) returns the logical value supported that indicates whether the custom workspace reader specified by obj supports the variable specified by var. When you write a custom workspace reader, you must implement the supportsVariable method in the class definition.

Input Arguments

expand all

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

Example: MyCustomReader

Workspace variable to import, specified as a variable in the base workspace.

Example: myVar

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | string | struct | table | cell | categorical | datetime | duration | calendarDuration | fi
Complex Number Support: Yes

Output Arguments

expand all

Whether the custom reader supports variable, returned as a logical value.

Examples

expand all

Write the function definition for the supportsVariable method to return true only when the reader can import the input variable. Specify the code for the supportsVariable 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). The supportsVariable method returns true when:

  1. The input variable is a structure or array of structures that contains the appropriate fields.

  2. The n field of each structure contains a character array or string to represent the signal name.

  3. The t field of each structure is a column vector of double data.

  4. The d field of each structure contains numeric data and is the same size as the t field, meaning there is a sample value for each time step.

classdef ExcelFirstColumnTimeReader < io.reader

  methods
    % ...

    function supported = supportsVariable(~, val)
      % Support structure with fields t (time), d (data), and n (name)
      supported = ...
        isstruct(val) && ...
        isfield(val,'t') && ...
        isfield(val,'d') && ...
        isfield(val,'n');
      if supported
        for idx = 1:numel(val)
          varName = val(idx).n;
          time = val(idx).t;
          varData = val(idx).d;
                    
          % Name must be string or character array
          if ~ischar(varName) && ~isstring(varName)
            supported = false;
            
          % Time must be double column vector
          elseif ~isa(time,'double') || ~iscolumn(time)
            supported = false;

          % Data size must match time size
          else
            timeSz = size(time);
            dataSz = size(varData);
                        
            if ~isnumeric(varData) || ~isequal(dataSz, timeSz)
                    supported = false;
            end
          end
        end
      end
    end

  % ...
  end
end

Version History

Introduced in R2020b