a matlab m file generating square wave when converted as user defined function in simulink dosent gives a square wave but gives as many outputs as number of samples

2 views (last 30 days)
I have a code to generate a square wave as follows:
fs=1000;
f=2;
t=0:1/fs:1-1/fs;
y=square(2*pi*f*t);
with a plot command as follows
plot(t,y,'r','LineWidth',2)
it gives the expexted square wave plot
But
when I use the code to create a simulink block and connect its output to scope instead of square wave as obtained in plot in matlab it diaplays as many output signals as equal to number of samples (1/(1/fs)). so if fs is 10 it displays 10 output signals each having a value equal to y during each sample.
if plot in matlab displays sqaure wave for 10 samples (fs=10) with values 1 1 1 -1 -1 1 1 1 -1 -1 the scope displays 10 signals with values 1 1 1 -1 -1 1 1 1 -1 and -1 .

Answers (1)

Taru
Taru on 23 Nov 2022
Hi Girish,
I gather that you are getting multiple square wave plots on the output scope when you run simulation through Simulink as you execute using the MATLAB function block.
You can look for the parameters, Amplitude and Frequency, that are assigned for the square wave each time Simulink performs sampling and make sure that they are static, i.e., they are called only once in the entire execution of the simulation.
Hope it resolves your query.

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!