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

25 ビュー (過去 30 日間)
Huck Febbo
Huck Febbo 2016 年 1 月 19 日
編集済み: Walter Roberson 2019 年 3 月 1 日
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
Pavithra Ashok Kumar 2016 年 1 月 22 日
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 Center および File ExchangeBlock and Blockset Authoring についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by