Main Content


Read external file data into data source workspace

Since R2022b


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


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 base class.

Example: myCustomFileAdapter

Workspace that contains the data from an external data source, specified as a 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.


Type of error, specified as an enumeration value from the enumeration class 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.



To learn about attributes of methods, see Method Attributes.


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.


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.
    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));
            item = item.getNextSibling;

Version History

Introduced in R2022b