MATLAB: Does the Simulink function in a Stateflow chart give variable undefined errors for variables defined in the chart

functionsimulinkstateflowundefinedvariable

I have a Simulink function defined in a Stateflow chart and it is trying to use the input to the Stateflow chart (e.g. "x") as the value of a Constant block. When I use "x" in the Simulink function I get an error saying that "x" is not defined. How do I fix this?

Best Answer

Simulink functions defined in Stateflow charts do not have access to the Inputs and Outputs of the Stateflow chart containing them. You can not, for instance, have a Constant block with value "x" if "x" is only defined as an Input to the Stateflow chart. If you want to use values from your Stateflow chart inside the Simulink function you need to pass them to the function through Inports instead. Similarly if need to get output from the function to use in the Stateflow chart you need to use an Outport. Adding Inports and Outports will add additional arguments and outputs to the function that can be given when calling the function in Stateflow.