Main Content

Simulink.data.DataSourceWorkspace

Contains data for external data source

Description

A Simulink.data.DataSourceWorkspace object is a handle object that can contain data from an external data source.

When you load data from an external source, for example by using the loadVariablesFromExternalSource function, Simulink® passes a reference to a Simulink.data.DataSourceWorkspace object to the custom file adapter that can read that external file format. The file adapter, derived from the Simulink.data.adapters.BaseMatlabFileAdapter class, loads the data into the workspace in the form of MATLAB® variables and data objects. The workspace object acts as a cache for Simulink and can run a MATLAB script, similar to a base workspace. Symbol resolution includes symbols in the workspace. See run.

Creation

Only Simulink can create and manage a Simulink.data.DataSourceworkspace object, but you can create another handle for the workspace object by assignment.

copyWks = dsWks
copyWks = 
DataSourceWorkspace with no properties

To test a custom file adapter without creating a data source workspace, use the Simulink.data.adapters.AdapterDataTester object.

Object Functions

clearVariablesClear specified variables from data source workspace
clearAllVariablesClear variables from data source workspace
getVariableReturn value of variable in data source workspace
getVariablesReturn values of variables in data source workspace
hasVariablesCheck if variables exist in data source workspace
listVariablesList variables in data source workspace
runExecute the specified MATLAB script in data source workspace
setVariableSet variable in data source workspace
setVariablesSet variables in data source workspace

Examples

collapse all

Define a getData method for an XML file adapter to populate the data from an XML file into the data source workspace that Simulink passes to the method.

This getData method reads an XML file in the following format:

<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