Use logged signal in Matlab Level-2 S-function (in Simulink) - matlab

Is it possible to use logged signals within Matlab Level-2 S-functions?
I need an S-function to run only every 5 s, so that that could be its rate. However, if I use the S-function to log the data, I need to call it at every time step and that slows down the code considerably. Is it possible to use logged signals within the Matlab Level-2 S-function instead?
Thank you in advance for the help!

Related

call a level-2 sfunction via the matlab editor

I have developped a simulink model. I compiled it to have a level-2 sfunction : 'sfun.mexw32'.
I would like to call this sfunction in a .m file in the Matlab editor.
I struggle with how to implement it, so I am first trying to understand how to do that with an illustration sfunction already existing in matlab : timestwo.
You can get more information by typing open('sfuntmpl.m') which is the template for sfunction.
[sys,x0,str,ts] =timestwo(0,0,1,3)
I got this error :
"Error using timestwo
MEX level2 S-function "timestwo" called with too many left hand arguments"
I do not understand why I have this error, and I expected to have the result of the input 1 which should be 2.
And also know if it is possible to give a vector of input and an input representing time to simulate a signal and get the result.
If your s-function has a .mexw32 extension then it is a compiled Level-2 C-code S-function. It is not an m-code S-function.
I think that you'll find that there is no possible way to call either a Level-2 M-code S-function, or a Level-2 C-code S-function from anything other than a Simulink model. A level-2 m-code S-function has one input, which is a block object created and passed to it by the Simulink execution engine, while a level-2 c-code S-function is comprised of a number of functions each of which is passed a simStruct (c-code) structure, which is also created by the Simulink execution engine.
You would have to create a model with your S-function in it, presumably along with the appropriate inport and outport blocks, then use sim to call the model.
Note that the link you give (i.e. open('sfuntmpl.m')) is to the template for a Level-1 m-code S-function. These can be called from MATLAB as they are just a regular m-code function. If you have a Level-1 m-code S-function (which it appears you do not) then you should be able to call it in the way that you are trying to do in the question.
The template for a level-2 m-code S-function is: edit('msfuntmpl.m');
The template for a level-2 c-code S-function is: edit(fullfile(matlabroot,'simulink','src','sfuntmpl_basic.c'));

Simulink model in a while loop

i want to get values for a simulink model. The aim is to implement the simulink model in a while loop in a matlab script
The simulation has to run 10s. The output of the simulink model is a signal value, which shouldnt exceed the value 1.
The while loop should run, till the right values a,b,c and d are find.
If the simulation is wrong, the value should increment. (a is outer loop and d the inner) All possible combinations should be run.
How it can be implemented, is it possible and how to get with possible errors in the simulink model?
How to get the simulink function data live (If it saved to the workspace, it appears, after the simulation (10s) is done)? I would like to get the results during the run in the workspace.
Have someone experience with it?
Thank you in advance!

Sample sequence of points from continuous signal simulink

I have a Matlab function (created by me) that must be evaluated only at a given rate. I would like to sample the value of a signal, give to this function (discrete values) and then, the calculated output must be hold until the next value is available. Is there a way in simulink to do this? All answers I have found use quantizer + ZOH but in this case I still get "a continuum" (or almost it) of points to be evaluated by thsi function which is really slow. Changing the rate of simulink's solver is also not an option as the result of this function will be given for a continuous time system.
Any help will be highly appreciatted!
Thank you
Assuming by Matlab function you mean a MATLAB Function block, then it sounds as if all you need to do is make the block discrete. Do that by right-clicking on the block, going down to Block Properties and then in the resulting dialog enter your required sample time.
The block will then sample its input and generate an output (which is held between sample times) at each sample time.

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.

Simulink performance with S-Function

I am trying to run my simulink file which have pid controller connected to s-function block.
When i set three values parameters of pid which are proportional, integral and derivatives it takes too long to run the whole process. Why this is happened?
In the dialog box of my pid diagram, for porportional value, its equal to the value which correspond to the constant amplitude oscillation.
Then for integral, its equal to Kcu/Ti. Ti is the ultimate period(Pu)/2 and
lastly for derivatives, its equal tu Kcu*Td and Td is Pu /8. This is refer to ziegler nichols method. and again my question is why it takes too long to running this file?
MATLAB S-functions are slow because they run in the MATLAB interpreter. Consider implementing it using Simulink blocks or using a "Embedded MATLAB Function" (pre-R2011a) or "MATLAB Function" (R2011a+) block.
Read Guy and Seth's thoughts on Simulation performance.
#Nzbuu is right about the Matlab S-functions.
But I think the problem here could be somewhere else: #Syarina are you saying that the Simulink simulation gets slower after you set the proportional coefficient for the controller? If you simulate the plant alone, in this case the S-function, do you notice a significant difference in the execution speed? If it is really so, I suppose the PID controller makes the ODE system stiff. This means that the different states of the ODE system have really different dynamics - some are very fast, some are very slow. Using an ode-solver that is not suited for stiff equations you will find the simulation much slower (actually you would have luck if it converges at all).
My suggestion is try to change the solver - for example ode15s.