Structure as output of MATLAB Function Block through Simulink.Bus object - matlab

I have a MATLAB Function Block where I compute X variables that I want to use later in a script.
My first option was to use To Workspace blocks for each of them, but since I have X variables, it fills up the model with blocks. The problem is that these X variables have different dimensions, otherwise I could just build a matrix.
My idea was to build a structure within the MATLAB Function Block and then use a single To Workspace block, but my research told me I would need a Simulink.Bus object to do so.
Is this approach at all the best way to achieve my goal? Thanks in advance for your input!

Related

Heterogeneous data from workspace into Simulink

I have different matrices to import into a Simulink Matlab function from the workspace. These matrices have all different dimension, which I don´t know at priori.
At the beginning I tried using the block 'constant' putting the data all together in a structure like this:
But then, I cannot pick the right matrix since I don´t know the dimension of each element (and also 'mux' cannot be used to split matrices).
I think I will have the same problem also with the block 'from workspace'.
I was wondering if there is a smart way to import heterogeneous structures like these. I tried also with cell-arrays, but it seems to be not supported by Simulink.
Thanks for any suggestions.
If the data is to be used in a Matlab Function block you could define the workspace matricies as parameters in the model explorer and in the Matlab Function port editor. You then have them accessible inside that function without even needing the "const" blocks or drawing any signals.
Even if your final intent is not to have data into a Matlab Function block those blocks are quite useful for extracting signals from heterogeneous data since you can do some size/type checking in them. Then you can output "simulink friendly" signals for use elsewhere.

Matlab Function inside simulink

I need to use a Matlab function inside of a Simulink model. I know how to use a Matlab function to do simple stuff. But what I need now is a little bit more complicated. Let me give you a basic example.
Assume that I need to have a block to generate a sine wave to be viewed directly on the scope (I know that there is already a sine-wave block, I'm just taking that as an example). If I'm writing in Matlab NOT in Simulink, I would do something like:
t = [0:1/30000:0.2];
A = 1;
f =10000;
y = A*sin(2*pi*f*t);
plot(t(1:100),y(1:100))
How can I build the same function inside a Simulink matlab-function block and see the results directly on the Scope?
Remember: The Matlab Function Block has two ports, u and y. Which represents input and output respectively. In the above-given example, a sine-wave generator doesn't need an input.
Perhaps it's just that you haven't chosen a very good example, but there are several things to be aware of when translating the code you've given into Simulink.
The easiest way to get the simulation time into a MATLAB Function block is by feeding a Clock block into an input port (which as #Daniel indicates, are optional, but in this case I suggest would be used for t). So I think you do want an inport in this example.
Your use of plot in your example only plots the first 100 points, where as a Simulink Scope rolls through the data being displayed. There's no concept of only displaying the first X points in a Scope when the simulation runs for longer than that.
You need to remember that Simulink generates data one simulation time step at a time, so you can't generate them all (as per your MATLAB code) and then plot them all. (Well... you can if you want to use frame based signals, but I assume that's not what you're asking here.)
So, to implement the equivalent of what you have would involve doing the following.
write a MATLAB Function block containing the following code (although you might want to make A and f input parameters rather than hard coding them)
function y = myCustomSineWave(t)
A = 1;
f =10000;
y = A*sin(2*pi*f*t);
Feed a Clock block into the above block, and have a Scope block on its output

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.

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