Runtime drawing subsystem icon based on output results - simulink

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.

Related

Write to DataStore from Matlab script

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.

How do I input variables into a Simulink model through MATLAB script (SimDriveline)

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.

MATLAB GUI callbacks and function variable scope

I think I have a basic understanding of scope, but I am a bit confused as to how this would work in a MATLAB GUI.
For instance, if I had a GUI that did a surf plot on an axes based on input from editBox1, I would have:
An updateAxes function that would update the axes with input from editBox1 (str2double(get(handles.editBox1, 'String'))).
An editBox1 callback that would call the updateAxes function.
Does it violate the idea of scope for updateAxes to directly use the get function? Should I be passing in the content of editBox1 as a parameter to updateAxes?
It is good to think about what you really need and not pass all data around. Still, handles is, in my opinion, something that can be passed around. You should also think that even if the handles are not passed around, you still have access to them. Figures are in essence global, since any figure (and thus figure properties as well) can be accessed through any function. By not passing handles around you can make it harder to access the figures, but cannot prevent access. If you have in mind to modify a figure, I can see no reason not to pass the handle to figure, or the handles to all objects in the figure. The second option will spare you some headache having to search the (recursive) list of Children in case you find out that you would need another handle to the same figure. As stated in a comment the GUI matlab have a simple way of handling this through the function guidata. Also, thinking about maintainability. Would it be easier to maintain code with a selected set of handles, properly named or sorted, or would you rather have to access them through a nameless set of Children?

How can I change a Block parameter in Simulink with Serial Port?

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.

How can the output of a Simulink block be fed back as an input?

I have 2 embedded Matlab functions which I am using to create a Simulink model. Both functions use the output of the second function as their input. I am getting an error at the moment indicating that this is an invalid loop.
Does anyone know how to implement this type of behaviour?
You've created an algebraic loop, which means that to compute the inputs of the Embedded MATLAB block are directly dependent on the outputs of the block. This is not allowed when the loop is a "self-loop", i.e. there is only one block in the loop.
One way to fix this is to put Unit Delay block(s) somewhere on the signal feeding back into the Embedded MATLAB block. See the documentation on algebraic loops for more information.