Main Content

setVariable

Set variable in data source workspace

Since R2022b

    Description

    example

    setVariable(dsWks,varName,varValue) assigns varValue to the variable varName in the data source workspace represented by the Simulink.data.DataSourceWorkspace object dsWks.

    Examples

    collapse all

    Define a getData method for an XML file adapter to read the data from an XML file and then use the setVariable function to populate the data 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

    Input Arguments

    collapse all

    Data source workspace, specified as a Simulink.data.DataSourceWorkspace object.

    Variable name specified as a string or character array.

    Example: "x"

    Data Types: char | string

    Variable value, specified as a scalar.

    Example: 2

    Version History

    Introduced in R2022b