Struct as input to a Simulink MATLAB Function block

316 visualizzazioni (ultimi 30 giorni)
Hi!
I want to simulate a physical system with the scalar state x, the parameters data and the differential equation dx/dt = dxdt(x,data), e. g.
function xDot = dxdt(x,data)
xDot=data.a+data.b*x;
I want to call this function from a Simulink Function block.
How can I hand the data struct into the Simulink block? Is there maybe a better way to store the model's parameters? Due to the type of the model's parameters a named way to store different types of parameters would be great. Having a vector to store the parameters is only my last resort.
Thank's in advance!

Risposta accettata

Johannes Köppern
Johannes Köppern il 29 Ago 2018
I found the answer myself on https://de.mathworks.com/help/simulink/ug/parameter-arguments-in-matlab-function-block-functions.html: In the MATLAB Function Block Editor, add an argument to the function header of the MATLAB Function block. The name of the argument must match the name of the masked parameter or MATLAB variable that you want to pass to the MATLAB Function block.
The new argument appears as an input port on the MATLAB Function block in the model.
In the MATLAB Function Block Editor, click Edit Data.
Select the new argument.
Set Scope to Parameter and click Apply.
The input port for the parameter argument no longer appears in the MATLAB Function block.

Più risposte (4)

TAB
TAB il 29 Ago 2018
Modificato: TAB il 29 Ago 2018
Check below links:
To connect the structure input or output in a MATLAB function with Simulink, you must define a Simulink.Bus object in the base workspace. Then use this bus object as signal datra type for the signals which are to be connected to Matlab function.

Johannes Köppern
Johannes Köppern il 29 Ago 2018
Thank's alot! That is what I was looking for!

Johannes Köppern
Johannes Köppern il 29 Ago 2018
Hi there, once again!
I was able to implement a bus containing the static parameters from my struct in the workspace.
Since this made quite some work necessary I ask you, if there is an even easier way to hand static (constand) parameterst to a Matlab function within Simulink. Still I want to keep the data in a greater super-structure so I don’t get to many inputs to my Simulink function?
Can you think about a method?
Once again, thanks in advance!
  2 Commenti
TAB
TAB il 30 Ago 2018
Modificato: TAB il 30 Ago 2018
If you have parameters on base workspace then you can access them directly in Matlab function (just like in stateflow).
  1. Open Model Explorer
  2. In Model Hierarchy window--> Select you s-function
  3. Add data --> Change data name to same as parameter name --> Select "Scope" as "Parameter".
Nicolò Binda
Nicolò Binda il 24 Apr 2020
Modificato: Nicolò Binda il 24 Apr 2020
Hi
I've got a structure in matlab and a bus with the same Hierarchy linket to an entity in simulink (i generate the bus from the struct and i selected in the entity generator as entity type "bus" and i choose the bus.
now i need to import data from the struct to the bus (so to each entity generated) but i'm not able to do this.
many thanks
Nicolò

Accedi per commentare.


Johannes Köppern
Johannes Köppern il 30 Ago 2018
Thank's! That's exactly what I was looking for.

Categorie

Scopri di più su Simulink Functions in Help Center e File Exchange

Prodotti


Release

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by