Thread Subject: Multiple generation of function calls in singular time step

Subject: Multiple generation of function calls in singular time step

From: Matthew

Date: 27 Jul, 2010 03:53:04

Message: 1 of 8

Hi MatLab wizards

I'm trying to generate a function call from within a MATLAB Embedded Function (within a Discrete Event Subsystem) using the following script (using Simulink and SimEvents):
-----------------
function fcn(time,matrix)

A=matrix;
B=time;

for i=1:1:length(A)
    if A(i)==B;
        GenerateCall()
    end
end

-----------------
In this case the 'time' variable is driven by the SimuLink standard clock function and the matrix is a constant of size 1x10.

The function call is then fed to an Event Based Entity Generator and counted.

While the Matrix is only 10 long, I seem to be generating 20 entities. Instrumenting through using eml.extrinsic('disp') shows it seems to be looping twice, when I only want it to execute once.

How do I stop this from happening? Is there a better way to achieve the same result?

cheers

Subject: Multiple generation of function calls in singular time step

From: Walter Roberson

Date: 27 Jul, 2010 04:05:11

Message: 2 of 8

Matthew wrote:

> function fcn(time,matrix)
>
> A=matrix;
> B=time;
>
> for i=1:1:length(A)
> if A(i)==B;
> GenerateCall()
> end
> end

Is your code perchance equivalent to

function fcn(time,matrix)
   if any(matrix == time)
     GenerateCall();
   end
end

Subject: Multiple generation of function calls in singular time step

From: Matthew

Date: 27 Jul, 2010 06:30:11

Message: 3 of 8

Walter Roberson <roberson@hushmail.com> wrote in message <YVs3o.31364$o27.29374@newsfe08.iad>...
> Is your code perchance equivalent to
>
> function fcn(time,matrix)
> if any(matrix == time)
> GenerateCall();
> end
> end

Hi Walt,

Unfortunately no, because the above code only would generate one function call in a singular time step. Sorry, I should have explained that my 1x10 matrix is equivalent to:

[0;0;0;0;0;0;0;0;0;0]

So I am trying to generate 10 entities at time zero or any combination of simulateous entities at any timestep. The above matrix with my code produces 20 function calls because the matrix is looped twice but why its looping twice I'm not sure.

Using your code I 'should' only produce 1 entity with the above matrix but I still had the problem of two entities being created.

Thank you for your help and I look forward to your response

Matt

Subject: Multiple generation of function calls in singular time step

From: Matthew

Date: 27 Jul, 2010 06:58:04

Message: 4 of 8

I think I have solved the problem though. Taking the embedded mathlab function from inside the Discrete Event Subsystem seems to have stopped the double generation.

So I can conclude that the double generation is due to the operation of the Discrete Event Subsystem, although how it managed to cause this error I would still be interested in learning so that I don't inadvertently make the mistake somewhere else.

Matt

Subject: Multiple generation of function calls in singular time step

From: Steve Amphlett

Date: 27 Jul, 2010 06:59:05

Message: 5 of 8

"Matthew " <mcdmj96@yahoo.co.uk> wrote in message <i2ludj$nh2$1@fred.mathworks.com>...
> Walter Roberson <roberson@hushmail.com> wrote in message <YVs3o.31364$o27.29374@newsfe08.iad>...
> > Is your code perchance equivalent to
> >
> > function fcn(time,matrix)
> > if any(matrix == time)
> > GenerateCall();
> > end
> > end
>
> Hi Walt,
>
> Unfortunately no, because the above code only would generate one function call in a singular time step. Sorry, I should have explained that my 1x10 matrix is equivalent to:
>
> [0;0;0;0;0;0;0;0;0;0]
>
> So I am trying to generate 10 entities at time zero or any combination of simulateous entities at any timestep. The above matrix with my code produces 20 function calls because the matrix is looped twice but why its looping twice I'm not sure.
>
> Using your code I 'should' only produce 1 entity with the above matrix but I still had the problem of two entities being created.
>
> Thank you for your help and I look forward to your response
>
> Matt

Does a function call subsystem not do what you want?

Subject: Multiple generation of function calls in singular time step

From: Devdatt Lad

Date: 27 Jul, 2010 14:16:26

Message: 6 of 8

Hi Matt,

If any input of the Embedded MATLAB (EML) Function block is connected to a
SimEvents block, then you will need to place this EML Function block inside
a Discrete Event Subsystem to ensure correct event-based operation.

Without your model, I cannot be sure what was going on before, and why you
were getting double firing. But my guess is that your Discrete-Event
Subsystem has two input ports. An update at each input port causes the
Discrete-Event Subsystem to execute. Move your Clock block inside the
Discrete Event Subsystem if this is indeed the case, and you will get the
expected behavior.

If not, try using the SimEvents Debugger using:

>> sedebug <your_model_name>

and step through the simulation. You should be able to identify why the
Discrete-Event Subsystem fires twice and not once as expected.

Hope this helps,
Devdatt

--
Devdatt Lad
The MathWorks, Inc.


"Matthew " <mcdmj96@yahoo.co.uk> wrote in message
news:i2m01r$7kn$1@fred.mathworks.com...
>I think I have solved the problem though. Taking the embedded mathlab
>function from inside the Discrete Event Subsystem seems to have stopped the
>double generation.
>
> So I can conclude that the double generation is due to the operation of
> the Discrete Event Subsystem, although how it managed to cause this error
> I would still be interested in learning so that I don't inadvertently make
> the mistake somewhere else.
>
> Matt

Subject: Multiple generation of function calls in singular time step

From: Matthew

Date: 28 Jul, 2010 06:13:05

Message: 7 of 8

"Devdatt Lad" <Devdatt.Lad@mathworks.com> wrote in message <i2mpnr$2qe$1@fred.mathworks.com>...
>
> Move your Clock block inside the
> Discrete Event Subsystem if this is indeed the case, and you will get the
> expected behavior.
>
> Devdatt Lad
> The MathWorks, Inc.
>

Thanks Devdatt that is exactly what the problem was, however, having moved the clock inside the Discrete Event Subsystem I am now getting an error telling me that the clock has a continuous sample time and needs to be constant/inherited to work within the block.

How do I set the clock to inherent the sample time of the simulation?

I have checked all block parameters and the simulation configuration but can't seem to narrow down where to change it.

I can arrange to send you a simplified version of the model if it helps.

Regards

Matt

Subject: Multiple generation of function calls in singular time step

From: Matthew

Date: 28 Jul, 2010 07:03:04

Message: 8 of 8

> How do I set the clock to inherent the sample time of the simulation?
>
> Regards
>
> Matt

I seem to have fixed this one too by using a Digital Clock with inherited sample time rather than the other Simulink clock.

Thankyou everyone for your help

Matt

Tags for this Thread

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.

rssFeed for this Thread

Contact us at files@mathworks.com