Plotting in Matlab using Real Time data from Simulink - matlab

I'm trying to solve a problem of simulating in real time in Simulink (This is solved) but plotting (real time) in Matlab ?
Details:
I want to be able to run a Simulink simulation (which is running in real time) and be able to turn on / off manual switches while the simulation is happening. This works well when I'm using the built in Scopes in Simulink but now I want to export that data to Matlab in real time as well (To make a custom looking graph).
So is there a way, to export this data (it can be sampled if that is necessary) to Matlab and make a plot that is constantly updating. Meanwhile I can still manipulate the switches in Simulink and influence the simulation manually ?
Simulink is effectively running continuously until I stop it.
Thanks for the help!

There should be some kind of notification going when simulink updates the data to be visualized. Maybe this is the linkdata feature.
Another, worse, solution is the drawnow command to redraw the graphs continously (the latter could be unnescessary costly for you program).

Related

MATLAB error handling of SIMULINK (sim) due to external component

My problem is that I am running a sweep study with some parameters. I change them in MATLAB and then run a simulation in MATLAB with the sim command. The simulation in SIMULINK contains an external software component from COMSOL with an FEM behind it. In some cases where the configuration is unfavorable (rapid changes in the feedback loop of the controlled system), the FEM tool is not able to converge in finite time, resulting in an error after a few simulation seconds/minutes/hours. The simulation command is inside a try-catch section to avoid that the program is stopped and the next study step starts to process the acquired data.
The problem is that in case the simulation has a convergence problem, the data generated up to this error is not sent to the workspace, but would be valid, and so I cannot find out from the data what was the reason for the convergence problem.
As additional information, I have been using the ToWorkspace block to send the data to my workspace where I do my post processing, and that works fine when the simulation is complete.
If any of you have any ideas on how to solve this problem, maybe you can help me.
Thank you in advance.

How to take values generated from a MATLAB program and display them in a Simulink block?

I have a working MATLAB program hooked up to an Arduino and rotary sensor that displays the current angle. I would like to display this angle on a Simulink model, so I can control a motor based off the current angle. Is there any way to do this without creating an angle sensor in Simulink and just using my MATLAB code that already works?
Your question is not really clear and it depends if you are trying to do it before simulation or during simulation.
If you want to do it before the simulation, just create a constant block with a variable name that is taken from the workspace, and let your function to set that variable in the workspace.
If you want to do it during simulation, it is a little more difficult but still possible. Lets create a sample a MVCE. The simulink model mask.slx contains a constant the is set to 0, and this constant value is displayed in the display block on the right. The simulation time is set to inf, so when you play it, you must stop it manually.
It is possible to checge the value of the constant while the simulation is running using this simple Matlab call:
set_param('mask/Value_to_set', 'Value', '10')
you should also consider that the constanta must be a Tunable gain (it is by default).
(as you can see the simulation is running).
There are some additional (and surely better) solution you can use:
Include your Matlab function in a MATLAB User Defined Function block, and call it at each simulation iteration. If you have compilation issue you shall follow the coder.extrinsic way (here an example for fmincon)
Use the Simulink Support Package for Arduino Uno Hardware Add-on that is available in the Add on store.

Run a continuous Simulink model with real-time input?

I'm new to Matlab/Simulink but have a requirement to interface with a Simulink model from a piece of software.
At the moment I have created a simple Simulink function 'Inc' which has 1 input to an addition block with a constant 1 and an output. I have used To/From Workspace blocks and can run the simulation over a fixed time using a time/value vector input, such as simin = [0,0;5,0;5,1;10,1].
What I would like to do is run my model continuously? Reading Workspace values in real-time rather than a pre-defined time based vector?
I can't see how to set this up? Can Matlab/Simulink do this?
To summarise, I would like simin and simout to be single values i.e. simin = 1, then with the model running continuously(infinitely) at the next fixed step simout would update to simout = 2. simin changes would be made at the Workspace at varying intervals.
The short answer is you can't (easily) do that, that's not how Simulink works. The MATLAB workspace is read/accessed at the beginning of the simulation and passed to the Simulink engine, and even if the values in the workspace change before the simulation is finished, this is not taken into account by the Simulink model running.
There are ways to work around this, but if you are a novice to MATLAB & Simulink, be aware that these are fairly advanced techniques, and I would advise to familiarise yourself with Simulink first.
Have a look at these similar questions for suggestions of how to do what you want:
Stream data form MATLAB to Simulink
Problem of variable updating in workspace
How Do I Change a Block Parameter Based on the Output of Another Block?
Tuning block parameters at every time step in a simulation
Simulink Signal Viewing using Event Listeners and a MATLAB UI
Obviously, you need to change the simulation end time to Inf or some large number.

Is it possible to have slight differences when running the same Simulink simulation twice?

Assume that you have a Simulink simulation. You run it twice and consider a plot of a certain signal in time. Is it possible that there are small differences between the two signals?
A possible cause could be a variable-step solver.
Assuming all simulation parameters are the same between runs, if your simulation is complex, and uses custom blocks, such a thing is possible, if there is a bug in the initialization code.
With S-Functions, for example, you can chose if and how to reset internal state between simulation runs. With C code, it is easy enough to forget to reset something altogether, and end up with some uninitialized variable that contains garbage.
Another possibility is that something gets written into the workspace from the simulation, and is fed back into the simulation when it is initialized the next time.

a nonstop Running of my Simulink model

In order to plot the Eb/N0 versus BER performance of my model Simulink and bring out the output (which is a vector of bits) from simulink to workspace, I need to the complete simulation of my model. But, the simulation doesn't stop, it runs hours und hours without stop. Actually, I haven't unterstand this problem as a beginner in Matlab-simulink. But, I am quiet sure that I need to see the end of my work to be able to analyse the performance and then correct if there is a problem.
Thanks in advance
This might be what you want to access data during simulation:
http://www.mathworks.com/help/simulink/ug/accessing-block-data-during-simulation.html