0
0
MATLABdata~30 mins

Multiple outputs in MATLAB - Mini Project: Build & Apply

Choose your learning style9 modes available
Multiple Outputs in MATLAB Functions
📖 Scenario: Imagine you are analyzing sales data for a small store. You want to write a MATLAB function that calculates both the total sales and the average sale amount from a list of sales.
🎯 Goal: Create a MATLAB function that takes a list of sales amounts and returns two outputs: the total sales and the average sale amount.
📋 What You'll Learn
Create a vector called sales with the exact values: 100, 200, 150, 300, 250
Create a variable called numSales that stores the number of sales
Write a function called calculateSales that takes sales as input and returns two outputs: totalSales and averageSale
Call the function calculateSales with sales and store the outputs in variables total and average
Print the values of total and average
💡 Why This Matters
🌍 Real World
Stores and businesses often analyze sales data to understand total revenue and average purchase size, helping them make better decisions.
💼 Career
Data analysts and scientists frequently write functions that return multiple results to summarize data efficiently.
Progress0 / 4 steps
1
Create the sales data vector
Create a vector called sales with the exact values: 100, 200, 150, 300, 250
MATLAB
Need a hint?

Use square brackets [] to create a vector with the given numbers separated by commas.

2
Create a variable for the number of sales
Create a variable called numSales that stores the number of sales in the sales vector using the length function
MATLAB
Need a hint?

Use length(sales) to find how many elements are in the vector.

3
Write the function to calculate total and average sales
Write a function called calculateSales that takes sales as input and returns two outputs: totalSales and averageSale. Use the sum function to calculate total sales and divide by length(sales) for average sale.
MATLAB
Need a hint?

Define the function with two outputs in square brackets. Use sum to add all sales and divide by length(sales) for average.

4
Call the function and display the results
Call the function calculateSales with the sales vector and store the outputs in variables total and average. Then print both values using disp.
MATLAB
Need a hint?

Use square brackets to capture two outputs from the function. Use disp and num2str to print the numbers as text.