Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Declaring "global" variables in Level 2 M S-functions

80 views
Skip to first unread message

Erik

unread,
Jan 28, 2010, 6:36:06 PM1/28/10
to
I want to develop an s-function in M, but I'm running into a major disadvantage compared to C.

In C, you can declare global variables which are global WITHIN the s-function. e.g.

float X;
function1(void)
function2(void)
function3(void)
X = 5; //do calculations

In an M s-function, you can declare a global variable, but it is global to ALL of Matlab and Simulink, not just within the s-function. It seems like the work-around is to use the Dwork vector and pass the block handle into each function like so,

%declare the Dwork vector
block.Dwork(1).Name = 'X';
block.Dwork(1).Dimensions = 1;
(...and some more properties...)

function1(block)
function2(block)
function3(block)
x = block.Dwork(1).Data; %save to a local variable
x = 5; %do calculations
block.Dwork(1).Data = x; %save back into Dwork

Which works, but can become very cumbersome. If i have lots of "global" variables, my Dwork vector is large and evolves during code development. Each time I make a change to the indexing of the Dwork vector, I have to go through all of my subfunctions and change the indices.

For example, if X is now to be stored in the 2nd element of Dwork, block.Dwork(2).Data, now I need to manually go into function3 and update it to look like this,
x = block.Dwork(2).Data;
x = 5;
block.Dwork(2).Data = x;


In C, it is so simple to work with global variables. Is there a way to make this easier in M?

thanks,
Erik

Walter Roberson

unread,
Jan 28, 2010, 6:48:20 PM1/28/10
to
Erik wrote:

> function1(block)
> function2(block)
> function3(block)
> x = block.Dwork(1).Data; %save to a local variable
> x = 5; %do calculations
> block.Dwork(1).Data = x; %save back into Dwork
>
> Which works, but can become very cumbersome. If i have lots of "global"
> variables, my Dwork vector is large and evolves during code
> development. Each time I make a change to the indexing of the Dwork
> vector, I have to go through all of my subfunctions and change the indices.

baseoff = 1;
offsets.x_off = baseoff; baseoff = baseoff + 1;
offsets.y_off = baseoff; baseoff = baseoff + 1;

x = block.Dwork(offsets.x_off).Data
x = 5;
block.Dwork(offsets.x_off).data = x;

Now if you were to switch the calculation of x_off and y_off, x would go into
the second slot instead of the first and you would not have had to do anything
in your code except insert or reorder or delete the index variables in one
place, and pass the additional structure variable 'offsets' around.

If you declare a struct that describes the fields of Dwork, such as

DworkStruct = struct('x',[],'y',[],'stiffness',[])

then you can build the offsets structure automatedly:

fn=fieldnames(DworkStruct);
for K = 1:length(fn); offsets.([fn{K} '_off']) = K; end

Or of course you could just use a cell array listing the order:

DworkNames = {'x' 'y' 'stiffness'}
for K=1:length(DworkNames); offsets.([DworkNames{K} '_off']) = K; end

Erik

unread,
Jan 28, 2010, 8:52:02 PM1/28/10
to
Walter, thanks for the nice work-around.
0 new messages