How to set a parameter as a variable in a simulink block from Matlab?
12 views (last 30 days)
Show older comments
Hello everyone,
I am using a function in matlab that has as input a frequency. The idea is that the function modifies the cutoff frequency of a low pass filter (from a Simulink model) with the frequency provided as input. The Simulink model splits a time function in two new functions (one with frequencies below the cutoff frequency and the other with the frequencies higher than the cutoff frequency). The code from the second line to the fifth one, is meant to update the function that is going to be split (in case it has been modified)
So far I have come up with the following code:
function Create_Signal_PuissancekW(x)
load puissance_kW
puissance_kWx=transpose(puissance_kW);
time=1:length(puissance_kW);
signalbuilder('Division_Profil_Charge/ProfilCharge', 'set', 'Puissance', 'Group_1', time, puissance_kWx)
set_param('Division_Profil_Charge/Pass Bass','Wlo','x')
sim('Division_Profil_Charge','SimulationMode','normal');
The problem I have is that the function set_param() doesn't allow me to put a variable instead of a real value. I have tried with set_param(gcb, 'UserData', x) and even puting sim('Division_Profil_Charge','SimulationMode','normal','Pass Bass/Wlo',x) but none of them seems to work. Does anyone have an idea of what the problem might be?
Here is the Simulink model, in case it is necessary:

And this is the error displayed by Matlab when I try to run the function: Create_Signal_PuissancekW(0.00175)

Thanks a lot in advance!
0 Comments
Accepted Answer
Sebastian Castro
on 11 May 2016
I think you're using the set_param function correctly, in the form:
set_param(blockName,'paramName','variableName')
The problem appears to be that you're doing all of this from inside a MATLAB Function, which has its own workspace. However, for Simulink to pick up that variable x, it needs to be in the base workspace.
From a function, you can assign a variable in the base workspace using the assignin function. For example:
assignin('base','x',x)
The above line means you are assigning a variable named x in the base workspace whose value is that of your local function's variable (which is also called x in this case).
- Sebastian
4 Comments
Rik
on 2 Nov 2020
I couldn't just find the command assignin anywhere else, which solved my problem about loading data from Matlab-Workspace into base workspace
More Answers (1)
Dev Club
on 22 Jul 2019
Hello Sebastian! I would like to know can I set the one of the parameters of 4 pulse generators in my circuit below simultaneously by one single input variable?
eg. I have a constant block (0.9) on left side (which is duty cycle) which goes into code block, can you suggest any code which will set a particular parameter of pg1 (Pulse Generator) and rest of them simultaneously?
And if code can be written what will be at the output of code block?
Thanks!

0 Comments
See Also
Categories
Find more on Event Functions 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!