setVariable - Set variable in data source workspace - MATLAB (original) (raw)
Main Content
Set variable in data source workspace
Since R2022b
Syntax
Description
Examples
Define a getData method for an XML file adapter to read the data from an XML file and then use thesetVariable 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
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