how to change variable value in lookup table during simulation in Simulink?
Show older comments
Hello,
so I have a 2 table lookup; first table has: P (48 values) and time(1:1:48); and the second table has: load Q (48 values) and time(1:1:48);
at some time ; for example at time=6; i want to change the value of P to 9; how can change variable value at the same time of simulation running?
I tried to use Matlab function to change the value of P but it didn't work;

the code is:
function Pref= fcn(loadref,currenttime, P )
persistent loadrefold;
% Initialize persistent variable
if isempty(loadrefold)
loadrefold = 0;
end
if loadref ~= loadrefold;
updatetime = round(currenttime+1)
P(updatetime,1)=loadref;
loadrefold=loadref;
Pref=P;
else Pref=P;
end
end
2 Comments
Sam Chak
on 20 Feb 2024
Hi @Mounira
Based on your clear description, it seems that there are two Lookup Tables involved. You mentioned that each Lookup Table has two inputs to the block, one being the load and the other being the time. However, I only see the time input (Hour of the Day) in the Simulink model. Am I missing something?
Additionally, at time
, you mentioned that you want to set the value of Load to 9, which seems to be a fixed value not dependent on the Lookup Table. Do you intend for the Load to remain at 9 for time t > 6 until the end of the simulation?

jana nassereddine
on 20 Feb 2024
i have inside the lookup table; i have 2 inputs; i will show you a picture; as for th etime; no only for a specific time i want a specific value;

Answers (2)
Jon
on 20 Feb 2024
0 votes
You could use the step block, found under Simulink>Sources for this purpose
Fangjun Jiang
on 20 Feb 2024
0 votes
Usually, using Dashboard blocks allow you to change variable values interactively during simulation
7 Comments
Jon
on 20 Feb 2024
So, depends upon whether you want to do this interactively, or you already have a well defined time that you want to change the value. If you know when you want to change values then the step block will provide the functionality you need.
jana nassereddine
on 20 Feb 2024
i have a code running inside matlab function; and i have a model next to it; the model and the code have a commun variable; i am changing the variable from the code; but it is not changing in the model; i tried to use assignin('base', 'P', P); but it didn't work
Mounira
on 20 Feb 2024
Fangjun Jiang
on 20 Feb 2024
Edited: Fangjun Jiang
on 21 Feb 2024
a little bit unusual but still can be done.
At the 1-D Looup Table, specify "Input port" as the source of the "Table data".
The Constant block is used to provide this "Table data". In this simple example, both the "Table data" and the time "Breakpoints" are specified as 1:10.
Use a "Slider" block to link to the 6th value of the Constant block. You can see the value is controlled by the slider position, which you can adjust while the simulation is running, at any time.

In fact, the slider can be linked directly to the "Breakpoints" parameter of the 1-D Lookup Table block, without the need to pull it outside to the "T" port of the block. But this example is better at showing that this approach works, since the "Breakpoints" value can be easily displayed.
Mounira
on 20 Feb 2024
Fangjun Jiang
on 20 Feb 2024
Look at your question and title, where does MPC come from??
Fangjun Jiang
on 21 Feb 2024
If the value of "P" is created or modified by this MPC or the MATLAB function, then this solution would be perfect too. Make that "P" an output of this MPC or the MATLAB Function block, remove the Constant block, feed "P" to the "T" port of the 1-D Lookup Table block, making it a dynamic lookup table.
Categories
Find more on Controller Creation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!