0
0
MATLABdata~10 mins

Multiple outputs in MATLAB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Multiple outputs
Call function with multiple outputs
Function runs code
Assign values to output variables
Return outputs to caller
Caller receives multiple outputs
Use outputs separately
This flow shows how a MATLAB function can return multiple outputs, which the caller receives and uses separately.
Execution Sample
MATLAB
function [sumVal, diffVal] = addAndSubtract(a, b)
    sumVal = a + b;
    diffVal = a - b;
end

[x, y] = addAndSubtract(5, 3);
This code defines a function that returns two outputs: sum and difference of two inputs, then calls it to get both results.
Execution Table
StepActionVariablesOutput Variables AssignedResult
1Call addAndSubtract(5, 3)a=5, b=3None yetFunction starts
2Calculate sumVal = a + ba=5, b=3sumVal=8sumVal assigned 8
3Calculate diffVal = a - ba=5, b=3diffVal=2diffVal assigned 2
4Return outputs to callersumVal=8, diffVal=2x=8, y=2Outputs assigned to x and y
5Use outputs x and yx=8, y=2Nonex=8, y=2 ready for use
💡 Function ends after returning both outputs to caller
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
aundefined5555
bundefined3333
sumValundefined8888
diffValundefinedundefined222
xundefinedundefinedundefined88
yundefinedundefinedundefined22
Key Moments - 3 Insights
Why do we use square brackets [ ] when calling the function?
Square brackets tell MATLAB to expect multiple outputs. Without them, only the first output is returned (see execution_table step 4).
What happens if we ask for fewer outputs than the function provides?
MATLAB returns only the requested outputs. For example, if only one output is requested, only sumVal is returned (not shown here but implied by step 4).
Can we ignore some outputs when calling the function?
Yes, you can use ~ to ignore outputs you don't need, but the function still computes them internally (not shown in table but common practice).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the value of diffVal?
A2
B3
C8
D5
💡 Hint
Check the 'Output Variables Assigned' column at step 3 in execution_table.
At which step are the outputs assigned to variables x and y in the caller?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look for when 'Outputs assigned to x and y' happens in execution_table.
If we call the function without square brackets, what will happen?
AAn error occurs
BBoth outputs are assigned to x
COnly the first output is returned
DAll outputs are ignored
💡 Hint
Recall key_moments about output assignment and square brackets usage.
Concept Snapshot
Multiple outputs in MATLAB:
- Use [out1, out2, ...] = functionName(inputs)
- Function defines outputs in square brackets
- Caller receives outputs in order
- Use ~ to ignore outputs
- Without brackets, only first output returned
Full Transcript
This visual trace shows how MATLAB functions can return multiple outputs. The function addAndSubtract takes two inputs and returns two outputs: their sum and difference. The execution table walks through each step: calling the function, calculating outputs, and assigning them back to variables x and y in the caller. The variable tracker shows how each variable changes after each step. Key moments clarify why square brackets are needed to receive multiple outputs and what happens if fewer outputs are requested. The quiz tests understanding of output values and assignment steps. The snapshot summarizes the syntax and behavior for quick reference.