Function output not displayed on scope.
Show older comments
The following function block is supposed to output three sine waves X, Y & Z with a 120 degree phase shift between each along with Angle A. Only looking at one phase at present. The way I thought this works is I have a For loop that runs from 0 to 360 and with each itteration of the loop a point on the sinewave is produced. I have confirmed the equation for X using Excel and the correct values are shown in the Simulink viewer. The problem is that this sinewave is not being shown on the scope and I am not an experienced user of Matlab and cannot find the reason, help appreciated.

function [X,Y,Z, A] = ThreePhase(Magnitude,Frequency,t)
Angle = 1:1:360; % Array of Angles
W = 2*pi*Frequency; % Frequency = 50 W = 314.1593
Ts = t / 360; % Time per Degree Ts = 55.6 E-6
Y = 0;
Z = 0;
for index = 1:1:360
X = Magnitude * (sin( W * (Angle(index) *Ts) ) ); % Calculate a value for the given angle.
Q = [index, X]; % Test purposes only
disp(Q);
end
A = Angle;
4 Comments
Mathieu NOE
on 17 Oct 2025
why a for loop ? you can do the same function much simpler
Roy
on 17 Oct 2025
Mathieu NOE
on 20 Oct 2025
hello again
see my answer below
"Please elaborate on how you could do it much simpler"
The name MATLAB comes from MATrix LABoratory. It is often simpler to write MATLAB code in terms of matrices (or vectors or arrays): https://www.mathworks.com/help/matlab/matlab_prog/vectorization.html
function [X, Y, Z, A] = ThreePhase(Magnitude, Frequency, t)
A = 0:360; % fixed starting angle
W = 2 * pi * Frequency;
X = Magnitude * sin(W * A * t / 360);
Y = 0;
Z = 0;
end
Accepted Answer
More Answers (1)
Walter Roberson
on 17 Oct 2025
0 votes
The scope would show the output signal. The output is after the for loop has run. So the scope would show the final X, final Y, final Z -- corresponding to Magnitude * (sin( W * (Angle(360) *Ts) ) ), and 0, and 0.
It would not especially surpise me if Magnitude * (sin( W * (360 *Ts) ) ) came out as 0 as well.
The contents of MATLAB Function Blocks are evaluated once every time step. (The size of a timestep can be variable, depending on whether you configured fixed step solver or variable step solver.) It is the ending values of the outputs that become the output values of the signals. Everything in one invocation is considered to take place at the same time.
You should consider using a clock input block, and inside the MATLAB Function Block, calculate the outputs only for the current clock value.
2 Comments
Roy
on 18 Oct 2025
Walter Roberson
on 18 Oct 2025
Constant Block1 --->| Constant Block2 ---> |
| multiply block1 ---> Trig block (sin)-+ | multiply block2 ---> scope
Clock ------------->| |--------------------> |
Simulink will automatically evaluate this series of blocks repeatedly, with different times on the clock. The action will be to evaluate CONSTANT2 .* sin(CONSTANT1 .* TIME) and display the current result via the scope.
Categories
Find more on Sources 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!





