Is there a way to pass an object instance from MATLAB to Simulink? - matlab

I have created a class 'BinaryTree' in Matlab. I would like to pass an instance of this object to Simulink, and use the associated class methods in Simulink. For example, I would like to pass a function in the format of 'Binarytree' class to Simulink and iteratively evaluate this function using 'ComputeTree', which is a method defined in the 'BinaryTree' class, using the simulation values.
I have considered using Matlab System Objects. I was wondering if there are better-suited ways to meet my objective, or if there are any available examples using Matlab System Objects to do what is described above.

Why not just use the Matlab Function block from the library
You don't need to pass anything, you can use persistent memory if you need to keep track of previous values.

Related

How can I programmatically set the inputs and outputs of a Matlab Function block to other simulink blocks?

I'm trying to programatically create several Matlab Function blocks to have their input set to the output of a Mux block, and their output set to the signal port of an Output Switch block.
I've created some basic Simulink models in the past using the add_block and add_line functions, but it seems Matlab Function blocks work differently.
I know what I'm trying to do can be achieved since I've done it manually, but I need to achieve this programatically to create large models.
Thanks!
It should work. Consider for example a Simulink model with a constant block, a MATLAB Function and a Scope block. Try the following:
add_line('untitled','Constant/1','MATLAB Function/1')
add_line('untitled','MATLAB Function/1','Scope/1')

How to use a variable as parameter of a s-function in simulink

I want specify some parameters of a s-functions using a variable and not using a constant inserted manually.
I have tried to define global variables in matlab and using them as paramaters and it works fine.
Anyway, i want that a variable computed in simulink is used as parameter of an other s-fuction. Is it possible? If so, how can i do it?
One idea was to continue to use global constants in matlab e to update them from simulink but i'm not able to do it because the "to workspace" block transforms them in struct or array and i need a single value.
Edit:
To update the variable in the global variable in the workspace is not useful because it seems that simulink read the values at the beginning
The only solution I can think of would be to have the "variable" as one of the input signals to the S-function block, with the signal coming from some other part of the Simulink model where it's computed.
I have found a solution.
First of all, it's important to know that:
a s-function paramenter can be: a constant, a workspace variable, a matlab expression. In matlab expression are included calls to matlab functions.
simulink computes the value of each parameter at the onset of the simulation (it has sense!).
The solution is:
use a matlab function for computing the parameter. a matlab function can call an other simulink simulation. I tried it, even if in a very simple case, and it worked.
I know, it's a crazy solution but it is a solution. Warning: at the moment i'm not able to know if this crazy solution has some side effects on the simulation.
The best solution to this problem is to convert the paramenter in input of the s-function but i can't do it because it's a 3rd party sfunction

Structure as input for a Matlab function Block

I have a, in my opinion, little problem using a Matlab-Function Block in a Simulink Model.
I want one of the inputs to be a structure.
But that doesn't work so easily, I've come across
suggestions on other semi-related problems, which tell about defining Simulink.Bus and so on,
but to be honest I don't understand how this can be applied on my problem.
It seems simple:
Structure as input for a Matlab function Block
But I don't know how.
Sorry for the inconvenience.
Regards BZAD
Within Simulink you create a Bus signal using the Bus Creator Block. This can then be fed into a MATLAB Function block, but only if an accompanying Simulink.Bus object is created in the Base Workspace.
See Types of Structures in MATLAB Function Blocks and Create Structures in MATLAB Function Blocks and the links off those pages for more information.

RMS not supported in Matlab function inside Simulink

Simulink has a module called "Matlab Function," which allows you to create a custom function in a Simulink flow diagram.
I implemented a simple function in a Simulink Matlab Function module. My function contains a call to Matlab's built-in rms(). When I run the Simulink model, I get the following error:
The function 'rms' not supported for standalone code generation
If I remove rms from my Matlab Function in the Simulink model, the error goes away and the model runs flawlessly.
Questions:
Is there a way to use Matlab's rms in Simulink?
Are there many other native Matlab calls that can't be used inside Simulink?
I just wanted to clarify and expand upon some points made in learnvst's answer.
Even if you are simply trying to simulate a model containing a MATLAB Function block and are not explicitly attempting to perform code generation, you will still get the not supported for standalone code generation error.
As learnvst indicated, there are multiple restrictions on functions that can be used with code generation. However, if you just want to simulate your model, Simulink allows you to do this if you signify these "black-listed" functions as being extrinsic. This lets Simulink know that the functions will be used for simulation purposes only and won't be part of code generation.
In your particular case, add the following line of code somewhere before your call to rms:
coder.extrinsic('rms');
Declaring a function as extrinsic in a MATLAB Function is often useful even when you are performing code generation. For example, you may want to visualize your data using the plot command during simulation, but obviously would not need the plot command to be part of generated code.
Refer to this doc for more info on declaring functions to be extrinsic.
The not supported for standalone code generation part of the error suggests to me that you are trying to use a product like Matlab Coder to make an executable or native code. If this is the case, there are many naive calls that cannot be used directly in both core Matlab and the toolboxes. The coder products only support a subset of the language. More information can be found here . . .
http://www.mathworks.co.uk/products/matlab-coder/description2.html
As for your call to rms, it is only calculating the root of the mean of the squares. Try creating an alternative using something like . . .
sqrt(mean(x.^2))
...where x is the signal.

How can I call an m file in Simulink and put it to a block in my model?

How can I call an m file in Simulink and put it to a block in my model (without using an S function)? Does anybody have an idea? I'd really appreciate it.
If you're trying to apply a user-defined MATLAB function to Simulink signals, there are a few different ways to do this, depending on your objective. All options are available within the User-Defined Functions section of the Simulink library.
Use the MATLAB function block if you intend to generate code from your model. This block does have restrictions, the entire gamut of built-in MATLAB functions is not available.
Use the Interpreted MATLAB function block if you don't care about code generation, this block can make use of any functions.
Use the Fcn block if your m-file is trivial and contains a simple expression operating on the inputs. In this case you can type the expression directly into the block dialog and reference the input / output signals as shown in the documentation.
MATLAB Fcn block is the best solution to embed M-function file into Simulink model. However, be cautious which version of MATLAB you are using, e.g., with later versions of MATLAB Function Block can be implemented with M-function file with %#codegen and C compiler need to be with your MATLAB package. Good luck