필터 지우기
필터 지우기

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

조회 수: 11 (최근 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 CenterFile Exchange에서 Block and Blockset Authoring에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by