Thread Subject:
Declare global array in Matlab and Acess it in Simulink matlab function block

Subject: Declare global array in Matlab and Acess it in Simulink matlab function block

From: Kwok

Date: 31 May, 2012 15:31:25

Message: 1 of 4

I would like to share array in Simulink matlab function blocks. I used the Simulink.Signal object to share the array in several matlab function blocks but my array increment with time. How can I set the array to constant, which does not change with simulation time.

Below is what I did.

1)Initialize variable in matlab workspace
a=Simulink.Signal;
a.DataType = 'double';
a.Complexity = 'real';
a.Dimensions = [2 2];
a.SamplingMode='Sample based';
a.SampleTime = -1;
a.InitialValue='ones(2,2)';

2)My simulink model only consist of 2 blocks, which are matlab function block and display.
Also, array "a" is included in ports and data manager. My matlab function blocks is below.

function y=fcn
global a
a=a+1;
y=a;

When I run this simulation, I expect to see [2 2;2 2] on the display block but it is not the case. The value in the display block increments with time.

How should I change my code to do exactly what I want it to do?

Thanks!

Subject: Declare global array in Matlab and Acess it in Simulink matlab function block

From: Phil Goddard

Date: 1 Jun, 2012 23:01:32

Message: 2 of 4


You're saying use the global value for a, and adding 1 to each value, each time the block is called.
Why do you expect the value to remain constant?

Phil.

Subject: Declare global array in Matlab and Acess it in Simulink matlab function block

From: Kwok

Date: 2 Jun, 2012 17:16:41

Message: 3 of 4

"Phil Goddard" <phil@goddardconsulting.ca> wrote in message <jqbhkc$kag$1@newscl01ah.mathworks.com>...
>
> You're saying use the global value for a, and adding 1 to each value, each time the block is called.
> Why do you expect the value to remain constant?
>
> Phil.

Hello Phil,

I am unclear about my question. I would like to increment the global variable a at a rate that is different than the sample time of the matlab function block. Sometimes, I do want to increment the global variable but not every time step of the simulation.

Subject: Declare global array in Matlab and Acess it in Simulink matlab function block

From: Phil Goddard

Date: 5 Jun, 2012 03:45:07

Message: 4 of 4


> I am unclear about my question. I would like to increment the global variable a at a rate that is different than the sample time of the matlab function block. Sometimes, I do want to increment the global variable but not every time step of the simulation.

Then you need to either
- split the code out into another block that has a slower sample rate, or
- include code that checks for when an update should occur and only do an update at those times. I would do this with an S-Function not the MATLAB Fcn block.

Phil.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
simulink Phil Goddard 1 Jun, 2012 19:04:29
rssFeed for this Thread

Contact us