MATLAB Answers

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

19 views (last 30 days)
Huck Febbo
Huck Febbo on 19 Jan 2016
Edited: Walter Roberson on 1 Mar 2019
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.

Accepted Answer

Huck Febbo
Huck Febbo on 29 Jan 2016
Edited: Walter Roberson on 1 Mar 2019
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!

More Answers (1)

Pavithra Ashok Kumar
Pavithra Ashok Kumar on 22 Jan 2016
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 Comment
Huck Febbo
Huck Febbo on 25 Jan 2016
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!