Simulink function with variable-size output - long execution time + unstable behaviour

1 vue (au cours des 30 derniers jours)
Currently I am facing an issue with a custom MATLAB function inside a Simulink model. The problem originates from a Simulink model where I use a Pulse generator block to generate a square wave signal. I would like to supply the parameters to this block through Simulink signals instead of through the Block parameters dialog.
I figured that this is not possible, thus I went about to create a MATLAB function that brings the same functionality. I first created the function in MATLAB:
% User inputs
SignalFrequency = 5; % [Hz]
PulseLength = 0.050; % [s]
PhaseShift = 0.060; % [s]
SimDuration = 0.6; % Stop time of simulation [s]
NumberOfDatapoints = 1000; % Corresponds to fixed step size of simulation
% Calculation
SignalPeriod = 1/SignalFrequency;
DutyCycle = (PulseLength/SignalPeriod)*100;
t = linspace(0, SimDuration, NumberOfDatapoints); % Create time vector according to simulation settings
Signal = square(2*pi*SignalFrequency*t - 2*pi*(PhaseShift/SignalPeriod), DutyCycle); % Generate square wave signal, syntax: "x = square(t,duty)"
Signal(Signal<=0) = 0; % Clamp negative values of signal to 0
plot(t, Signal); % Visualise result
I translated this code into a custom MATLAB function inside Simulink, just to test whether this would work:
With the following underlying MATLAB code:
function VectorOutput = fcn(SignalFrequency, PulseLength, PhaseShift, SimDuration, NumberOfDatapoints)
% Generate signal
SignalPeriod = 1/SignalFrequency;
DutyCycle = (PulseLength/SignalPeriod)*100;
t = linspace(0, SimDuration, NumberOfDatapoints); % Create time vector according to simulation settings
sig = square(2*pi*SignalFrequency*t-2*pi*(PhaseShift/SignalPeriod), DutyCycle); % Generate square wave signal, syntax: "x = square(t,duty)"
sig(sig<=0) = 0; % Set zero level
% Write function outputs
VectorOutput = sig; % Size [1xNumberOfDatapoints]
As the Fixed-step size of the simulation can vary, the size of the function output vector can vary as well. It is in any case a 1xn vector, but the size of "n" can vary. I tried to set the size of VectorOutput in the Ports and Data Manager as follows:
But this really produces instable behaviour. Running the simulation in Simulink takes a couple of minutes and during this time, both Matlab and Simulink remain unresponsive to mouse clicks/commands. Does anyone know what I am doing wrong?

Réponses (0)

Catégories

En savoir plus sur Simulink Coder dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by