Discussion:
Problem of variable updating in workspace
(too old to reply)
Jadav Das
2009-01-14 18:59:02 UTC
Permalink
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
Phil Goddard
2009-01-15 03:27:02 UTC
Permalink
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.
Jadav Das
2009-01-16 01:02:02 UTC
Permalink
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
Post by Phil Goddard
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.
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.
Phil Goddard
2009-01-16 02:45:03 UTC
Permalink
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.
rach
2012-05-29 15:01:06 UTC
Permalink
Hi Jadav,

Did you manage to figure it out in the end? I am currently facing the same problem as what you have posted. However, it is my first time using simulink and am having a pretty hard time getting things to work.

May I know how exactly did you get it resolved using the S function? I have no idea how the S function can be used, despite reading the online help.

Thank you so much for your help and I really look forward to your reply.

Best Regards,
Rachel
Post by Jadav Das
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
Post by Phil Goddard
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.
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.
Mads
2014-04-28 17:20:12 UTC
Permalink
Hi Phil,

Can you please tell me which document are you referring to by "Simulink->Getting Started->Working with Blocks-Working with block parameters->Changing the Value of Block Parameters During Simulation"?

Thanks for your help.

Regards, Mads
Post by Phil Goddard
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.
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.
Loading...