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

simulink call blocks from s-function

17 views
Skip to first unread message

pavt

unread,
Aug 11, 2009, 12:53:59 PM8/11/09
to
Is there a way to call other blocks from an s-function in simulink or
bundle several blocks into an s-function?

Phil Goddard

unread,
Aug 11, 2009, 11:31:19 PM8/11/09
to

> Is there a way to call other blocks from an s-function

That depends what you are trying to do.
Can you get/set parameters of another block -- yes, see
>> doc set_param
and
>> doc get_param

Can you get another block to execute in some way (independently of the part of the model that the s-function is executing in) then no.

> in simulink or
> bundle several blocks into an s-function?

In the context of what an s-function is then this question doesn't make sense.
An s-function is a block -- just like other blocks it (generically) has inputs, outputs and states.
You could incorporate the code from several s-functions into a single s-function, but then you have a single s-function not a bundle of s-functions.

Phil.

pavant

unread,
Aug 13, 2009, 3:46:57 PM8/13/09
to
On Aug 11, 11:31 pm, "Phil Goddard" <philNOS...@goddardconsulting.ca>
wrote:

Thanks for the reply. What I meant by bundling blocks is can I create
a block using s-function that is similar to a subsystem in some ways.
I had so many of these tiny blocks each with their parameters and it
became too difficult to handle. So, I converted most of the model to a
few s-functions which is way easier to handle and experiment with the
model. But I can't implement the functionality of a continuous time
delay in an s-function and that is preventing me from converting a
whole subsystem into a single s-function block. So, I have several
copies of these subsystem in my model and I have to keep changing the
dialog parameters in some 10 delay blocks in 5 subsystems every time I
want to try something with a different delay. (I sort of avoided the
problem by using a variable delay block and centrally changing the
constant delay in m-file, but am not very happy with it)

Any further tips would of great help to me.

Phil Goddard

unread,
Aug 13, 2009, 7:59:05 PM8/13/09
to
Given that a subsystem is something that has inputs, outputs and states then you _could_ think of an s-function as a subsystem, but that would be unusual as they are thought of, and discussed as, blocks.

There are also some subtleties that need to be considered:
A (plain vanilla) subsystem is a virtual block, i.e. it doesn't appear in the block sorting order.
Rather the blocks that are inside the subsystem are in the sorted list and the order that they appear in the list _may_ change from one simulation to the next if Simulink decides that's a sensible thing to do (and assuming that you have made changes to the model between simulations).
In that respect an S-function would need to be considered as an Atomic Subsystem.

Time delays can certainly be implemented in an S-function.
For an example that uses a random time delay see
http://www.mathworks.com/matlabcentral/fileexchange/22666

The following is a best practice that all models should use, but in your case seems particularly relevant:

- don't type numeric values into block parameter, instead type the name of a MATLAB variable.
- before running the simulation give the variables used in the model a value in the MATLAB workspace. This would normally be done by running a MATLAB script that creates all the variables.

In your case where you have many structurally identical subsystems that use different parameters then use a MATLAB structure for your data:
subsystemData(1).param(1) = ...
subsystemData(1).param(2) = ...
...
...
subsystemData(n).param(1) = ...
subsystemData(n).param(2) = ...

Phil.

pavant

unread,
Aug 18, 2009, 4:09:04 PM8/18/09
to
On Aug 13, 7:59 pm, "Phil Goddard" <philNOS...@goddardconsulting.ca>
wrote:

> Given that a subsystem is something that has inputs, outputs and states then you _could_ think of an s-function as a subsystem, but that would be unusual as they are thought of, and discussed as, blocks.
>
> There are also some subtleties that need to be considered:
> A (plain vanilla) subsystem is a virtual block, i.e. it doesn't appear in the block sorting order.
> Rather the blocks that are inside the subsystem are in the sorted list and the order that they appear in the list _may_ change from one simulation to the next if Simulink decides that's a sensible thing to do (and assuming that you have made changes to the model between simulations).
> In that respect an S-function would need to be considered as an Atomic Subsystem.
>
> Time delays can certainly be implemented in an S-function.
> For an example that uses a random time delay seehttp://www.mathworks.com/matlabcentral/fileexchange/22666

>
> The following is a best practice that all models should use, but in your case seems particularly relevant:
>
> - don't type numeric values into block parameter, instead type the name of a MATLAB variable.
> - before running the simulation give the variables used in the model a value in the MATLAB workspace.  This would normally be done by running a MATLAB script that creates all the variables.
>
> In your case where you have many structurally identical subsystems that use different parameters then use a MATLAB structure for your data:
> subsystemData(1).param(1) = ...
> subsystemData(1).param(2) = ...
> ...
> ...
> subsystemData(n).param(1) = ...
> subsystemData(n).param(2) = ...
>
> Phil.

Thanks again Phil. But the example implementation that you pointed has
discrete time states while mine involve continuous time states and I
am not sure if it would be as easy.

0 new messages