Accessing parameters of input signal in a custom MATLAB function in simulink
6 次查看(过去 30 天)
显示 更早的评论
I am trying to access the parameters like Amplitude, Period of the input pulse generator signal in side the custom Matlab function block of Simulink. The get_param function is producing an error. Its there a way to input these parameter as methods of a structure or something of that sort because i want to analysis the Amplitudes of three different pulse in the function block
0 个评论
回答(1 个)
Riya
2025-6-20
Hi Jyotirmay,
I understand that you are trying to access parameters such as Amplitude and Period of multiple Pulse Generator blocks inside a custom “MATLAB Function” block in Simulink, and encountered an issue while using the “get_param” function.
This issue arises because “get_param” is not supported within “MATLAB Function” blocks, as these blocks are intended for code generation and do not support Simulink runtime functions like “get_param” or “set_param”. Attempting to use such functions will result in errors during model simulation or code generation.
To resolve this, you can follow one of the approaches below:
Option 1: Pass Parameters as Inputs
You can extract the values of Amplitude, Period, etc., from each Pulse Generator block and pass them as separate input ports to the MATLAB Function block.
For example, if you have three Pulse Generator blocks:
function out = analyze_pulses(u1, u2, u3, A1, P1, A2, P2, A3, P3)
% u1, u2, u3 are the three pulse signals
% A1, P1, etc., are Amplitude and Period for each pulse
% Example analysis
totalAmplitude = A1 + A2 + A3;
out = totalAmplitude;
end
Use Constant blocks or similar to feed Amplitude and Period values as inputs.
Option 2: Use a Simulink.Parameter Struct
Define a structure in the MATLAB base workspace and mark it as a tunable parameter.
pulseParams.A1 = 5;
pulseParams.P1 = 10;
pulseParams.A2 = 3;
pulseParams.P2 = 20;
pulseParams.A3 = 4;
pulseParams.P3 = 15;
pulseParamsObj = Simulink.Parameter(pulseParams);
pulseParamsObj.CoderInfo.StorageClass = 'ExportedGlobal';
Then, in the “MATLAB Function” block, declare a parameter params:
function out = analyze_pulses(u1, u2, u3, params)
A1 = params.A1;
A2 = params.A2;
A3 = params.A3;
out = A1 + A2 + A3; % Example logic
end
To configure this, open the block, go to the Symbols pane, and set params as a Parameter.
For further reference, here is the documentation for supported functions inside MATLAB function blocks:
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Sources 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!