matlab stateflow vs. Rhapsody - matlab

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.

Related

When to use "to workspace" block vs "outport" block

I can use both blocks for getting output from simulink to matlab, but if there are two of them there should be difference in the way they are used but I can not figure it out.
Using an Outport block allows you to,
use your model as a Model Reference
when generating code (using Simulink Coder) from the model, interface the model with other code
If you're not needing to do either of the above - for instance you only want to dump data to the MATLAB Workspace - then the To Workspace block is arguably easier to use. Plus it shows the user what the resulting variable will be without them having to open the model properties window.
Note also that To Workspace can be used at any level of model hierarchy, whereas Outport can only be used to get data out of the highest level of a model. (Outport are used in SubSystems but to interface a sub-level of a model to a higher level, not to get data out of the model.)

How to programmatically configure the tunability of model parameters?

I'm porting a large Simulink model from Simulink R2010a → R2017b.
The main model is basically a glue-layer for many interwoven reference models. My objective is to generate a standalone executable out of this main model using Coder.
Parameter tunability in this context is not done via the Signals and Parameters section on the Optimization tab in the Model Configuration Parameters dialog (as is the case in stand-alone models), but rather, via constructing Simulink.Parameter objects in the base workspace, and referencing those in the respective referenced models, or in their respective model workspaces.
Now, AFAIK, in R2010a it was enough to set
new_parameter.RTWInfo.StorageClass = 'Auto';
new_parameter.RTWInfo.CustomStorageClass = 'Define';
to make the parameter non-tunable and convert it into a #define in the generated code. In R2017b, this is no longer allowed; the StorageClass must be 'Custom' if you set a non-empty CustomStorageClass:
new_parameter.CoderInfo.StorageClass = 'Custom'; % <- can't be 'Auto'
new_parameter.CoderInfo.CustomStorageClass = 'Define';
But apparently, this does not make the parameter non-tunable:
Warning: Parameter 'OutPortSampleTime' of '[...]/Rate Transition1' is non-tunable but refers to tunable variables (Simulation_compiletimeConstant (base workspace))
I can't find anything in the R2017b documentation on making parameters non-tunable, programatically; I can only find how to do it in stand-alone models via the dialog, but that's not what I want here.
Can anyone point me in the right direction?
NOTE: Back in the day, Simulink Coder was called Real-Time Workshop (well, Real-time Workshop split into Coder and several other things), hence the difference RTWInfo vs. CoderInfo. Note that RTWInfo still works in R2017b, but issues a warning and gets converted into Coderinfo automatically.
In generated code it should appear as #define, the way you specified it.
https://www.mathworks.com/help/rtw/ug/choose-a-built-in-storage-class-for-controlling-data-representation-in-the-generated-code.html
Btw, yes, it's a bit confusing, because in m-file you specify CustomStorageClass = 'Define';, in GUI you specify Storage class as Define (custom), but in documentation they say Storage Class as Defined.
I am not sure why warning about tunability shows up.

Matlab/Simulink - Create S-function from DSpace Code

I know that it is possible to create S-function from C code that I provide.
But is it possible to create one S-function from C Code, which is generated from a Simulink Model for dSPACE ECUs, with low effort. The reason is, I am trying to test the dspace code with matlab/simulink.
Furthermore I'm aksing because the generated code from this simulink model consists multiple .c and .h files and I don't know how to integrate these files in one s-function block.
There are several methods of doing this. I suggest using the Legacy Code Tool as its structure helps to guide you through the process. You could also explore calling the function(s) from Stateflow or a Matlab Function block depending on the application.
With respect to multiple c/h files. I assume they will all need to be visible to Matlab through pointing to the source paths, but you should be able to identify the 'entry' function that you're interested in and utilize that in your model.

Simulink find() does not follow resolved links

I have a Simulink model "mod" with a Subsystem "link" in it, which is actually an active (i.e. "resolved") library link. Inside this linked Subsystem there is a Stateflow Chart "chart".
In Simulink I can search for the Stateflow Chart like this:
find_system('mod', 'FollowLinks', 'on', 'Name', 'chart')
But when I have the model as object "modObj", I can also use its "find" method:
modObj.find('Name', 'chart')
Unfortunately the "find" method does not follow resolved library links by default. Also the documentation does not describe a parameter like "FollowLinks" for "find_system". I already tried using "FollowLinks" and "-followlinks" but without success. Also searches on the net gave no result.
Is there an undocumented parameter for this functionality?
I know that I can use the "find_system" function for the above example. But in fact I am searching for Stateflow objects inside models, which are only found by the "find" method.
It seems that it is not possible to navigate through linked Stateflow objects. I have now worked around this by implementing a method which detects linked Stateflow charts (by using the "ReferenceBlock" property of the Simulink handle), ensuring the linked library is loaded and returing the library's object. Then I am able to work on the library instead of the current model (although it might be locked, but searching and property extraction is always possible).

MATLAB GUI to set Object Constructor

I'm currently designing a GUI to open a MAT file(s) to store the time-series variables in it as properties in a class, and do things to the data within the class. The Class is pretty well defined, however, it is the I/O portion of it that I need guidance on.
The constructor of the class currently is written to set the properties to empty if there are no inputs. However, I have a method that has a UIOPEN to get the file and set the properties of the class.
I have created a GUIDE GUI, where I pass the empty object of the class to the handles of the GUI and store it using guidata. The GUI has a button (along with other elements) called "Get File" that will call the property setter method when clicked to populate the properties and a listbox with those properties.
The uncertainty is in the whole architecture of what I have done. I'd like to know if there is a better way to accomplish this. It seems like it's kind of a mickey mouse way of doing it. Thanks!
I can point out a few things:
GUIDE is old, weird and it is generally better to create GUIs programmatically
once you have gotten rid of GUIDE you can have the object govern the GUI behavior easily - the object could have methods to spawn and refresh the gui, hold handles to gui elements in private properties etc.
Creating object-manged guis can also save you some problems with syncing workspace with the gui without using global, assignin or evalin. Since the object holds the data and it also governs the gui - the problem is no longer present.
Mathworks disencourages using guidata for things other than handles to graphical objects, and advices the user to use appdata