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

  • Follow


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!
0
Reply kkc5034 (8) 5/31/2012 3:31:25 PM

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.
0
Reply phil986 (301) 6/1/2012 11:01:32 PM


"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. 
0
Reply kkc5034 (8) 6/2/2012 5:16:41 PM

> 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.
0
Reply phil986 (301) 6/5/2012 3:45:07 AM

3 Replies
55 Views

(page loaded in 0.075 seconds)


Reply: