how to create a custom s-function in simulink? - matlab

What is the block that I can use for the creating a custom s-function in simulink?
I want to use C code using S-Function.. I know that there are functions that I have to define but I don't know how to reach it or which code to start with exactly..
Note: I am totally new to matlab and simulink..

Check out the S-function Builder block from Simulink's library (in Simulink->User Defined Functions). You can set number of states, inputs, outputs and also add code for calculating the derivatives + outputs. When you press Build it generates C code and you can start with that as a template. Or you could use the S-function Builder interface for writing all your code.
Edit: Matlab also has a built-in template that you can access:
edit([matlabroot,'/simulink/src/sfuntmpl_basic.c']);

Did you check the MATLAB documentation on this?
It is usually pretty complete, once you find the right page to read.
For s-functions there e.g. is
http://www.mathworks.de/de/help/simulink/sfg/how-to-implement-s-functions.html
which includes links to example s-functions you can find in your matlab-installation.

Related

Using coder.extrinsics conditionally

This question refers to Matlab coder extrinsic functionality. Some functions like fprintf are extrinsic in older Matlab version, and are not extrinsic in the newer ones. Is there a way to support several Matlab versions, if coder.extrinsics is allowed only at the top level, and it's not possible to put it under if statement?
You cannot conditionally make some functions extrinsic directly. One way would be to use two different functions like fprintf_old and fprintf_new. fprintf_old would have coder.extrinsic declaration and then calls fprintf. fprintf_new can call fprintf without extrinsic declaration. Now you can pick between the two calls by checking for your version with a condition that is constant during compilation. For example,
if coder.const(isOlderVersion())
fprintf_old();
else
fprintf_new();
end
In code generation, feval constructs an extrinsic call to the function named in the first argument. Since you can embed calls to feval inside of control flow, it can be used to selectively call a function extrinsically and keep the code in a single source file:
if isOlderVersion()
% Call fprintf extrinsically
feval('fprintf');
else
fprintf();
end

Matlab Simulink function

I am building a reduced order observer in MATLAB. The matrices are calculated using functions/script files outside matlab and simulink function blocks are using these functions to calculate values.
The problem is that some commands like 'acker', 'place' etc which used to work on command window/function/script files are not working in simulink function block and showing errors.
I tried using simin block to take these matrices from workspace but it is also showing errors which I can't understand.
Thanks for your help.
If I get your question correctly then, from User defined functions, you could add a Matlab function block with the following code:
function fcn(in)
%#codegen
coder.extrinsic('acker', 'place')
# Now you can use acker, place so add more code.

Use of idss data in Level-2 S-function

I am trying to write a level-2 S-function for use in system identification live during a simulation using the n4sid function. However, it does not appear that the output of the n4sid function, an idss datatype, is supported in the output. Is there a way to output this data type from an S-function? Alternatively, can this data type be used internally in an S-function and fed directly into another Matlab function inside the S-function code? Thanks.
Is there a way to output this data type from an S-function?
No.
Alternatively, can this data type be used internally in an S-function and fed directly into another Matlab function inside the S-function code?
Yes.
You can create data/destroy data/call functions etc just like any other piece of MATLAB code.

Getting the solver type and step size (for fixed step solvers)

we are trying to integrate a simulation model into Simulink as a block. We have a custom continuous block which loads an m file that contains the functions Derivatives, Outputs etc.
My question is: is there a way to find out which solver is used currently and with which parameters? Our model won't be able to support variable time solvers and I would like to give a warning. Similarly, the model requires the fixed step time for initialization.
Thanks in advance.
You can get the current solver name using
get_param('modelName', 'SolverName');
Some of the other common solver parameters are
AbsTol
FixedStep
InitialStep
ZcThreshold
ExtrapolationOrder
MaxStep
MinStep
RelTol
SolverMode
You can find other parameters you may wish to query by opening the .mdl file in your favorite text editor and digging through it.
If I'm understanding your use case correctly, you are trying to determine the type of solver (and other solver params) for the top-level simulink system containing your block.
I think the following should give you what you want:
get_param(bdroot, 'SolverType'); % //Returns 'Variable-step' or 'Fixed-step'
get_param(bdroot, 'FixedStep'); % //Returns the fixed step size
Notice that for purposes of generality/reusability, this uses bdroot to identify the top-level system (rather than explicitly specifying the name of this system).
If you want to find out more about other model parameters that you can get/set, I would check out this doc.
Additionally, I'm interested to know why it is that your model doesn't support a variable-step solver?

how can this function "resizeColumnscore" resizes image?

I want to know how can this function(from MATLAB) resize the columns of an input image using weights an indices previously computed.
Which equations uses to do that?
resizeColumnsCore(double(in), weights', indices');
When I looked for a function called resizeColumnsCore in MATLAB 7.11.0 (R2010b) I didn't find anything. However, I did find a MEX-file by that name in MATLAB 7.8.0 (R2009a) in this subdirectory of the Image Processing Toolbox:
C:\Program Files\MATLAB\R2009a\toolbox\images\images\private\
I guess they've phased it out or replaced it with another function in newer MATLAB versions. Now, if you want to know what the MEX-file does, you need to look at the source code it is compiled from. Luckily, it appears that this source code resizeColumnsCore.cpp can be found in the following directory:
C:\Program Files\MATLAB\R2009a\toolbox\images\images\private\src\misc\
And you can look through that code to determine the algorithms used to resize the columns of an image given a set of weights and indices.
Now, if you want to know how these input arguments to resizeColumnsCore are computed, you'll have to look at the code of a function that calls it. I know of at least one function in the IPT that calls this function: IMRESIZE. If you type edit imresize at the command prompt it will open that function in the Editor, allowing you to look through the code so you can see how the arguments to resizeColumnsCore are created.
What I can tell you for R2009a is that there is a subfunction in the file imresize.m called contributions which computes the weights and indices that are ultimately passed as arguments to resizeColumnsCore. That is where you will want to start looking to determine what algorithms are used to compute these arguments.
Looks like this isn't a proprietary MATLAB function. Could we see some code or a link to the code?