MATLAB Stateflow Variable Scope Issues - matlab

I'm trying to use a variable, defined in a stateflow transition, within one of the states of the stateflow chart. However, I'm getting a "Variable ____ does not exist" error. How can I make the state recognize the variable? It's defined within the data inspector as a local variable, if that helps.
Stateflow chart - the transition in question is from state 1-2
State 3 (compression) - I need to use these variables in this state

Related

Is it possible to declare a persistent variable in a Stateflow subchart?

I'm using a Stateflow chart to generate some code (C action language). I would like to declare a subchart variable as persistent (or static), so the value is remembered the next time the subchart is executed.
A solution is to attach this variable to one of the parent states, but then this variable is visible to all the subcharts, which is not ideal.
Another solution is to create an embedded Matlab function with persistent variables on it, but this is too cumbersome, since I would need to read all the variables in the beginning and write them before leaving the state.
Is there a simpler way to achieve this?
Yes. Open Model Explorer, go to the subchart, add data.

Error when using an inner variable of a component

I am using Thermal Power Library from Modelon. There is a condenser component in the Thermal Power Library which is used for the modeling of power plants.
The default heat transfer area for the wall_2 in the condenser component is 0.8*A_heat_tot, the variable of A_heat_tot is an inner variable in the condenser component, but when I try to use this variable, there is an error showing that this variable isn’t defined.
My question is that If I can use the inner variable directly. If not, how should I use it?
Short answer: You need to address the variable with its full path, i.e. wall_2.A_heat_tot.
A_heat_tot is define in StandardWall and can thus be referred to directly inside the class. However, when you are making changes to A_heat from outside the instance of StandardWall (i.e. outside wall_2) you must point to the origin of A_heat_tot since it is otherwise not known in the scope from which you are trying to use it.
Likewise, if you are making the modification in you simulation model (Preheater_Model_Validation2) you must use the full path, i.e. hex.wall_2.A_heat = hex.A_heat_tot
By the way, this has nothing to do with the inner qualifier in the Modelica language.

Access to "hidden" variables in Simulink

Some example models that are included in the Simulink library, include some variables that their value is not visible. For instance, in the model power_PVarray_grid_det the sample time of the powergui is defined as Ts_Power. I can not figure out the value of this variable. Is there a way to find these hidden variables, have access to them and change their value?
Thanks in advance

Control Variables in SIMULINk

I'm new to Matlab and SIMULINK, and I know that this might be easy. But I just can't find the answer on the internet.
I'm building a SIMULINK model (group of blocks) and I want to set the values inside the blocks as variables so I can maybe control it from an m file or something. How can I do this?
As #rayryeng has pointed out you can just type the name of a variable in place of the parameter value of blocks, and then whatever value that variable is set to in your Matlab workspace will be used.
Whenever I do this, I like to set default values of the variables in the models intialization callback function details here. That way your model is portable and will run on it's own.
In the simulink model,
In the 'Value' field of a Constant Block, enter the variable name. The constant block will then look like this: (see uplim and lowlim)
Now, whenever you wish to change the variable's value, execute the following commands through an m-file:
Let's assume the variable's name is pressure and the new value is 5.
assignin('base','pressure',5);
set_param('path of constant block','Value','pressure');
The path to a constant block (or any simulink block) looks something like this: modelname/Constant2 (considering it is the top level of your model; Constant Block number can vary)

matlab stateflow vs. Rhapsody

In Rhapsody - after creating a class, one can create many similar objects (instances) of that class.
In matlab's stateflow tool - one can create a state which is, as far as I know, an object.
Is there a possibility to use a state in matlab's stateflow tool as a class and create one or more objects of it?
I don't know if this is what you were asking, but yes you can create objects from stateflow classes.
As far as I know stateflow has many classes. For example, a stateflow state, a stateflow junction, stateflow function.
When you click to create a new state you are creating a new object of the state class. To do it programmatically you have to first get the handle of an stateflow object (this can be a whole stateflow chart or a stateflow chart for example).
To get this handle you can use the sfgco function - this function gets the handle of a stateflow object that is currently selected (in case none is selected, you get the handle to the stateflow chart object).
To create a new object of a class that has as parent a stateflow object that you have the handle you can use something like this:
Creating a new Stateflow state:
state = sfgco;
newState = Stateflow.State(state); %this creates a state inside the stateflow object selected by sfgco.
For more information look for the Stateflow API pdf file.
Stateflow charts are often used within Simulink, and reside inside an S-function (a block inside Simulink with time dependent inputs/outputs). Each S-function will then be an instantiation of the statechart. Normally these form a simulink model, with blocks connected by lines. Although I think it is also possible to do this dynamically, this would be far outside of the comfort region of the tools intended usage.
I've decoupled simulink (and stateflow blocks) from simulink models by using libraries. I can instantiate many of them (graphically) and update them separately from the models in which they
A Stateflow State is not an object. When generating code from Stateflow Charts, States are usually represented as a single "case" within a "switch" statement. They do not have properties or methods and can neither be instantiated.
Actually I am not aware that it is possible to create classes in Stateflow or Simulink at all. Both tools are intended to graphically represent algorithms and not classes, relationships or other (abstract) objects.