So what I'm trying to do is this: I have a simulink stateflow model. To display some stuff from this model I built a GUI. In this GUI I have a button that should set a flag to true when pressed, which I want to use inside my stateflow model to start a transition. So whenever I enter a certain state in the stateflow I set the value of the flag to false and I want it set to true when I push the button. Unfortunately I can't find any way to set a local parameter inside a stateflow from a GUI callback function. I've tried using datastore memory blocks, but even though I can set the value from inside the stateflow model, I can't set the value from the callback function of the GUI.
Help would be really appreciated!
I once built a similar GUI that I put in a test harness for a Simulink model.
To make this work and not intrude too much on the Simulink design I implemented as an "Level 2 Matlab S-Function".
This does imply som interfacing overhead (setting up the ports and their parameters), but you can get anything that you have as a signal in or out from the stateflow chart into such a block (you will probably need a z^-1 feedback loop if you both want to get the output and control input).
The GUI code can be called from the Start block and the values read in the "Update" segment. If the pushbutton is activated, save this in a variable that is evaluated in the Outputs function and then set the outport to indicate this event (with reset logic if needed).
Check the online help for Level 2 Matlab S-function and in particular the msfuntmpl_basic.m to get started.
Hope this helps. I would like to note that this requires that you limit your interactions to those for which the stateflow has input/output.
Related
For my coursework project in MATLAB, I have decided to build a drive-line model within Simulink, using the SimDriveline toolbox. The idea is to get the user to input values for the various parameters that are associated with each part of the model, such as the engine or the transmission. I would like to able to write this in a MATLAB script, but I'm not sure how to assign the values that are input to the Simulink model. For instance, the stock sdl_vehicle example that comes with SimDriveline. I am aware of the sim() command, but I am still confused on how to use it properly.
Also at the end of the simulation, the program is supposed to display the graphs that are collected in the scope window. I know that in the window itself that the scope can be printed to a figure, but is it possible to print that scope to a figure through MATLAB script?
This is the first time I have ever used a program like MATLAB. I would appreciate any help I could get, many thanks in advance!
There is a simulink block called simin:
http://de.mathworks.com/help/simulink/slref/fromworkspace.html?searchHighlight=simin
I used it some days back and it worked quite well. You can use the block in your model and define some signals/varibles as input.
After that you may write a Matlab-Script with an input function to set all the previous defined input values.
I am loading my Simulink Model to a microprocessor. After that I want to change a value of a Block parameter.
In the picture below the data comes to Matlab Function block is coming from Serial Port. Now, appearently th incoming data is assigned to input u of Matlab Function block. In that Matlab Function block I want to write a code that changes , for example, coefficient of s in the Transfer Fcn block. Or it can change any other parameter you can give in code hypothetically.
If anybody can give me any solution that would be ver much appreaciated!. Thank you in advance.
Substitute a custom-made subsystem for the transfer function whose parameters have to change, and create the subsystem from basic blocks so that coefficients can be read from signal sources. You might be interested in this
http://blogs.mathworks.com/seth/2011/03/08/how-do-i-change-a-block-parameter-based-on-the-output-of-another-block/
Even if you don't want to change parameters at every time step, the restriction
"[...] calls to set_param also preclude the model from ever being compiled
into a real-time application."
seems relevant to your case.
Anyway, as far as I know the post linked above still applies in 2014.
Hope that helps.
i have a constant block in a simulink model; i would like to change its value during the simulation when a particular event occurred in a stateflow chart. how can i do it?
If the Stateflow chart output indicates when this event has occurred, or if you can add an additional output that toggles when the event occurs, loop this indicator back (possibly through a Unit Delay block) to the control input of Switch block. The two inputs to the Switch would be two Constant blocks containing the two different values.
If you must do this programmatically without modifying the model, take a look at the reference for the sim command. I should warn you that this may not be trivial to accomplish.
Also, if want to be able to modify the contents of a Constant block mid-simulation (i.e. make it tunable), you may have to turn off Simulink's optimization settings that inline invariant parameters.
You can also add a Slider Gain block between your constant block and the Stateflow block. This would allow you to change the value of the slider gain block during simulation, without the need to change the value of the constant block.
I am trying to model a microwave in MATLAB's stateflow module. I have modeled states like on-off (microwave can be turned on/off). In 'on' state, I have sub-states like 'door open', 'door close' and 'cooking'. I have set conditions on transitions correctly. I was just wondering whether is it possible to provide cooking time as an input (from simulink) to this stateflow microwave model; exactly like we press the keypad on real microwave. This time will be taken as input and the stateflow chart will remain in 'cooking' until that time and the input time will decrement to zero
you can use a variable as cook_time in the states/transitions and while building the stateflow, it will detect it automatically, then you can specify it as Input from Simulink in the GUI.
You can find an example on how to pass an input from Simulink to state machines in Stateflow in the getting started videos here:
http://www.mathworks.com/products/stateflow/examples.html
Does someone know a way how to update/draw subsystem's input or output results in runtime? In this sense, one could do a Scope block, which updates itself during the simulation, so one could see the results already by looking at the block.
Of course, my intention is not making a scope block but make some custom drawings based on results inside the subsystem in runtime.
This would mean either to:
be able to access a variable with results in plot command of masked subsystem and making sure that Simulink calls refresh everytime the variable changes
change MaskDisplay from outside, for example by inputting absolute values in plot command and signaling to Simulink that it needs to refresh that Subsystem's drawing
One way of to use a MATLAB Function block and declare plot, for example, as an extrinsic:
coder.extrinsic('plot');
You can then use plot in the MATLAB Function. You can also do this for a custom MATLAB file:
coder.extrinsic('my_custom_draw_function');
Somewhat more complicated can be to use m-file s-function.