Thread Subject: Problem of variable updating in workspace

Subject: Problem of variable updating in workspace

From: Jadav Das

Date: 14 Jan, 2009 18:59:02

Message: 1 of 4

Hi everybody,
 I have few variables in matlab workspace at the start of simulation and I am using those variables in many blocks in the simulink. During simulation I want to update the values of the variables based on some calculated values in the simulink. How can I update the variables values at the workspace during the run time? Can anybody help me?

Thanks in advance.

Jadav

Subject: Problem of variable updating in workspace

From: Phil Goddard

Date: 15 Jan, 2009 03:27:02

Message: 2 of 4


There are possibly two issues here:
1. Updating the value in the workspace (which is what you ask about directly)
2. Using the updated values in the subsequent part of the simulation (which you don't directly ask about, but I am going to assume is really what you are wanting to do).

The standard Simulink blocks only write to the workspace when you pause or terminate the simulation.
So to update workspace variables during the simulation you'll need to write an S-Function.
If it's an m-code S-Function then you'll use MATLAB commands like
>> doc assignin
to do the actual writing.

By default Simulink only checks the workspace for values during model initialization at the start of the simulation.
To get it to look at the workspace again during the simulation you'll need to do a little more work.
The various techniques are listed in the following section of the doc:
Simulink->Getting Started->Working with Blocks->Working with block parameters->Changing the Value of Block Parameters During Simulation.

Most of them are manual techniques, but there is one sentence about using the function set_param which is the way to do it programatically.

Phil.

Subject: Problem of variable updating in workspace

From: Jadav Das

Date: 16 Jan, 2009 01:02:02

Message: 3 of 4

Hi Phil,
 Thank you very much for your quick respone. You are pointing out exactly what I am looking for. I am able to update the value at the workspace by using "assignin" command in the S-function. But to use those updated values in rest of the simulation is not working. I tried to implement the "set_param" command as you said in several places of block properties but did not. I might be missing something. Let me clear myself a bit more. I have several variable say 'a', 'b', 'c' etc with some initial values at the start of simulation and I used those variables in many many places of "Fcn" function blocks in the main/sub/sub-sub system. Now to update these values during run time I can use "assignin" command in S-function that update the workspace and to use those updated values where exactly I have to use the "set_param" command. Thanks for your valuable times.

Regards
jadav



"Phil Goddard" <philgoddardNOSPAM@telus.net> wrote in message <gkmae6$b9n$1@fred.mathworks.com>...
>
> There are possibly two issues here:
> 1. Updating the value in the workspace (which is what you ask about directly)
> 2. Using the updated values in the subsequent part of the simulation (which you don't directly ask about, but I am going to assume is really what you are wanting to do).
>
> The standard Simulink blocks only write to the workspace when you pause or terminate the simulation.
> So to update workspace variables during the simulation you'll need to write an S-Function.
> If it's an m-code S-Function then you'll use MATLAB commands like
> >> doc assignin
> to do the actual writing.
>
> By default Simulink only checks the workspace for values during model initialization at the start of the simulation.
> To get it to look at the workspace again during the simulation you'll need to do a little more work.
> The various techniques are listed in the following section of the doc:
> Simulink->Getting Started->Working with Blocks->Working with block parameters->Changing the Value of Block Parameters During Simulation.
>
> Most of them are manual techniques, but there is one sentence about using the function set_param which is the way to do it programatically.
>
> Phil.

Subject: Problem of variable updating in workspace

From: Phil Goddard

Date: 16 Jan, 2009 02:45:03

Message: 4 of 4


Assume that you have a model called 'untitled' and a constant block in it 'Constant' where the value is given my the variable C, then in your S-Function you'll want

assignin('base','C',whateverItsNewValueIs);
set_param('untitled/Constant','Value','C');

Note that the set_param command is not actually changing anything in the block; the parameter was C at initialization and it will be after the set_param, however by doing a set_param you are forcing the block to go back to the workspace to get the current value for C.

Or if it's a Gain block called 'nameOfMyBlock' with variable K as the gain value you'll want

assignin('base','K',whateverItsNewValueIs);
set_param('untitled/nameOfMyBlock','Gain','K');

In general you'll have
set_param('modelName/SubsystemName/SubSubsystemName/BlockName','BlockProperty',NewValue);

You can get the block properties in the doc from the section
Simulink->Model And Block Parameters->Block Specific Parameters

It looks like for an Fcn block you'll want
set_param('untitled/nameOfMyBlock','Expr','theExpression');

Note that not all parameters of a block are tunable.

I might also add that what you are doing here is an unusual thing to want to do, and my suspician is that you are tying to do something that would be better achieved by rewriting your model so that the value of a signal is changing and that signal is used to update how a block works.
My suspicions are even more highlighted when you say you're using an Fcn block, which is one that I don't recommend -- it's typically used by people who haven't spent time thinking about how to implement their algorithm in a better way.

With that, good luck...

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 li min 16 Jan, 2009 08:17:22
simulink Phil Goddard 14 Jan, 2009 22:30:21
rssFeed for this Thread

Contact us at files@mathworks.com