Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
variable scope in m code S-function

Subject: variable scope in m code S-function

From: pk68

Date: 17 Aug, 2012 15:45:12

Message: 1 of 3

I am writing an S function in m code. As part of mdlInitializeSizes() function I am defining certain variables as follows

function [sys,x0,str,ts]=mdlInitializeSizes()
  exports = zeros(1, n_export);
  p_exp = libpointer('doublePtr', exports);
  ...
  ...


When I try to extract these in mdlUpdate, I get an error "undefined function or variable 'p_exp' ".

function sys=mdlUpdate(t,x,u)
    exports = get(p_exp, 'Value');
  ...
  ...


Not sure what I am missing in setting up these variables such that they are visible in all functions. Any help is appreciated.

Subject: variable scope in m code S-function

From: Phil Goddard

Date: 17 Aug, 2012 18:43:15

Message: 2 of 3

Variables in each S-Function method has a local scope, so can't be seen in other functions.
The mechanism for doing what you require is to store the info in a Work Vector (in one or more of the functions) and then retrieve and use the Work Vector (in one or more other fucntion(s)).

See http://www.mathworks.com/help/toolbox/simulink/sfg/brd0tgs.html for general info on using DWork vectors.

I don't know what datatype you'll need to store a libpointer (or if one can be stored at all), but the available datatypes are given on
http://www.mathworks.com/help/toolbox/simulink/slref/simulink.blockdata.html#f29-108665

Phil.

Subject: variable scope in m code S-function

From: pk68

Date: 17 Aug, 2012 20:30:24

Message: 3 of 3

"Phil Goddard" <phil@goddardconsulting.ca> wrote in message <k0m3c3$9mo$1@newscl01ah.mathworks.com>...
> Variables in each S-Function method has a local scope, so can't be seen in other functions.
> The mechanism for doing what you require is to store the info in a Work Vector (in one or more of the functions) and then retrieve and use the Work Vector (in one or more other fucntion(s)).
>
> See http://www.mathworks.com/help/toolbox/simulink/sfg/brd0tgs.html for general info on using DWork vectors.
>
> I don't know what datatype you'll need to store a libpointer (or if one can be stored at all), but the available datatypes are given on
> http://www.mathworks.com/help/toolbox/simulink/slref/simulink.blockdata.html#f29-108665
>
> Phil.

Thanks Phil. I am able to pass variables through the Work Vectors.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us