How to set a parameter as a variable in a simulink block from Matlab?

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!

 Accepted Answer

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

Hello Sebastian,
Thanks a lot! Now it works perfectly. I have one last question that I think is related to the solution you gave: when I run the function for the first time, the variables that I named Bat_kW and SC_kW (in Simulink) aren't loaded to the workspace (I used the Simulink block "To Workspace") and they are only loaded when I run the Simulink model manually. Is this also a problem of using a function to run the simulation? If so, how can I put them into the base Workspace?
I have tried with:
assignin('base','Bat_kW',Bat_kW);
assignin('base','SC_kW',SC_kW);
but it seems to be not as simple as that.
Thanks again for your help,
Juan
Oh, my bad! I didn't notice that you were also running your model from that function. In that case, I got a better idea for you instead of using assignin.
When you run the sim command, you can tell the model to use the function's current workspace instead of the base. Then, you can freely use your variables.
sim('Division_Profil_Charge','SimulationMode','normal', ...
'SrcWorkspace','current');
... Now, when you use sim, all the data you log (outputs, signals, To Workspace blocks, etc.) gets shoved into an output variable. This is why you don't see it. So, this is what you want to do:
>> simout = sim('Division_Profil_Charge','SimulationMode','normal', ...
'SrcWorkspace','current');
>> Bat_kW = get(simout,'Bat_kW')
- Sebastian
Once again thanks a lot for your help. I use the second method you propose and indeed it works perfectly.
Comment posted as flag by Saeed Ahmadzadeh Mahboobi:
I couldn't just find the command assignin anywhere else, which solved my problem about loading data from Matlab-Workspace into base workspace

Sign in to comment.

More Answers (1)

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!
Capture.PNG

Categories

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!