Passing a structure of parameters into and S-function in MATLAB

14 次查看(过去 30 天)
I am trying to pass a structure of parameters to an S-function in MATLAB. I have a bunch of parameters and I would like to avoid passing them like this:
% The general form of an MATLAB S-function syntax is: % [SYS,X0,STR,TS,SIMSTATECOMPLIANCE] = SFUNC(T,X,U,FLAG,P1,...,Pn)
I would prefer passing a single structure that includes all of my parameters. I loaded the data into the Model Workspace as:
What should I do now?
Thanks!
Also,I do not want to use global variables.

采纳的回答

Huck Febbo
Huck Febbo 2016-1-29
编辑:Walter Roberson 2019-3-1
Setup 1: Load the data as a structure into the base workspace and run the simulink model
clear;
clc;
close all
PlantName = 'untitled';
open(PlantName)
TFinal = 10;
load DATA_HMMWV.mat
sim(PlantName, TFinal)
Setup 2: [![level 1 S-function that is being called in simulink][1]][1]
Setup 3: When you double click on this model, specify the structure that you would like to pass to the S-function as: [![enter image description here][2]][2]
Setup 4: your functions should also have the structure in it:
function [sys,x0,str,ts,simStateCompliance]=system1(t,x,u,flag,DATA_HMMWV)
and any other functions that you need the structure in, for example:
case 1
sys = mdlDerivatives(t,x,u,DATA_HMMWV);
then,
function sys = mdlDerivatives(t,x,u,DATA_HMMWV)
Now, you have passed a strucure to level-1 S-function!

更多回答(1 个)

Pavithra Ashok Kumar
I understand that you want to pass all the parameters as a struct using S-Functions. You can do this by using a "Bus" in the Simulink model. In the S-function builder( Check here for details ), navigate to "Data Properties>Input Ports". In the column "Bus", select "on".This would set the input type as "struct".Hope this helps.
  1 个评论
Huck Febbo
Huck Febbo 2016-1-25
Thank you for your response. I tried to do this as you said, but had a lot of difficulty. I am not that familiar with C code and Level-2 S functions. Currently, I am using a level-1 S function and would like to do it in that context. Why is this so difficult for me? I just want to pass a structure (defined by previously saved .mat file) to a level-1 S function in Simulink.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Simulink Functions 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by