Main Content

getData

Read external file data into data source workspace

Since R2022b

Syntax

diagnostic = getData(adapterObj,sourceWorkspace,prevChecksum,diagnostic)

Description

diagnostic = getData(adapterObj,sourceWorkspace,prevChecksum,diagnostic) populates the data from the external source file into a data source workspace.

Custom file adapters must define behavior for the getAdapterName, getSupportedExtensions, and getData methods. In addition, you can choose to override the isSourceValid, supportsReading, getSectionNames, open, and close methods.

Input Arguments

expand all

Custom file adapter, specified as an object of a class that derives from the Simulink.data.adapters.BaseMatlabFileAdapter base class.

Example: myCustomFileAdapter

Workspace that contains the data from an external data source, specified as a Simulink.data.DataSourceWorkspace object.

Previous checksum value, specified as a character vector or string. By default, the value is the timestamp for the last modified date of the external source file.

Diagnostic information from the adapter, specified as a structure. Specify the error information that your getData method returns by setting the fields of the empty diagnostic structure that Simulink passes in to the method. The structure has these fields.

FieldDescription
AdapterDiagnostic

Type of error, specified as an enumeration value from the enumeration class Simulink.data.adapters.AdapterError. These are the possible enumeration values.

  • NoDiagnostic (default)

  • GenericFailure

  • HardError

  • Inaccessible

  • AmbiguousSource

  • NoPermission

  • UnrecognizedFormat

  • Unsupported

DiagnosticMessageMessage returned as the error text, specified as a string. When AdapterDiagnostic is set to NoDiagnostic, the value is an empty string.

Data Types: struct

Output Arguments

expand all

Diagnostic information from the adapter, specified as a structure. For more information, see the diagnostic input argument.

Attributes

Abstracttrue

To learn about attributes of methods, see Method Attributes.

Examples

expand all

Write the function definition for the getData method to populate the data from the external source file into the data source workspace.

In this example, the getData method reads data from an XML file with a format similar to the following.

<customerData>
<x>10</x>
<y>15</y>
</customerData>

function diagnostic = getData(adapterObj, sourceWorkspace, previousChecksum, diagnostic)​
    % Each time getData is called on the same source, sourceWorkspace is the same as
    % the last time it was called. Clear it to make sure no old variables exist.
    clearAllVariables(sourceWorkspace);
    dom = xmlread(adapterObj.source);
    tree = dom.getFirstChild;
    if tree.hasChildNodes
        item = tree.getFirstChild;
        while ~isempty(item)
            name = item.getNodeName.toCharArray';
            if isvarname(name)
                value = item.getTextContent;
                setVariable(sourceWorkspace, name, str2num(value));
            end
            item = item.getNextSibling;
        end
    end
end

Version History

Introduced in R2022b