📖 Scenario: You work in an office where you need to handle sales data stored in Excel files. You want to read this data into MATLAB, process it, and then save the results back to a new Excel file.
🎯 Goal: Learn how to read data from an Excel file, modify it, and write the updated data back to a new Excel file using MATLAB.
📋 What You'll Learn
Read data from an Excel file named sales_data.xlsx
Create a variable to hold the sheet name Sheet1
Calculate the total sales for each product
Write the updated data with total sales to a new Excel file named sales_summary.xlsx
💡 Why This Matters
🌍 Real World
Handling sales or inventory data stored in Excel files is common in many businesses. Automating this with MATLAB saves time and reduces errors.
💼 Career
Data analysts, engineers, and scientists often need to import and export Excel data for reporting and analysis.
Progress0 / 4 steps
1
Read data from Excel file
Write a line of MATLAB code to read the data from the Excel file named sales_data.xlsx into a variable called data.
MATLAB
Hint
Use the readtable function with the filename as a string.
2
Set sheet name variable
Create a variable called sheetName and set it to the string 'Sheet1'.
MATLAB
Hint
Assign the string 'Sheet1' to the variable sheetName.
3
Calculate total sales
Add a new column called TotalSales to the data table by multiplying the columns Quantity and Price.
MATLAB
Hint
Use element-wise multiplication .* to multiply Quantity and Price columns.
4
Write updated data to new Excel file
Write the data table to a new Excel file named sales_summary.xlsx using the sheet name stored in sheetName. Use the writetable function.
MATLAB
Hint
Use writetable with the filename, the table, and the sheet name as arguments.
Practice
(1/5)
1. Which MATLAB function is used to read data from an Excel file into a table?
easy
A. csvread
B. xlswrite
C. readtable
D. load
Solution
Step 1: Identify the function for reading Excel files
MATLAB provides readtable to import Excel data as a table.
Step 2: Differentiate from other functions
xlswrite writes data, csvread reads CSV files, and load loads MAT files.
Final Answer:
readtable -> Option C
Quick Check:
Excel read function = readtable [OK]
Hint: Use readtable to import Excel data as tables quickly [OK]
Common Mistakes:
Confusing readtable with xlswrite
Using csvread for Excel files
Trying to use load for Excel data
2. Which of the following is the correct syntax to write a table T to an Excel file named data.xlsx?
easy
A. readtable('data.xlsx', T)
B. write(T, 'data.xlsx')
C. xlsread('data.xlsx', T)
D. writetable(T, 'data.xlsx')
Solution
Step 1: Identify the correct function for writing tables
The function writetable writes tables to Excel files.
Step 2: Check the syntax
The syntax is writetable(T, 'filename.xlsx'). Other options are either reading functions or incorrect.
Final Answer:
writetable(T, 'data.xlsx') -> Option D
Quick Check:
Write table syntax = writetable(T, filename) [OK]
Hint: Use writetable with table and filename to save Excel files [OK]
Common Mistakes:
Using write instead of writetable
Confusing xlsread with writing
Passing table as second argument to readtable
3. What will be the output of the following MATLAB code?
T = readtable('sales.xlsx', 'Sheet', 'Q1', 'Range', 'A1:C5');
disp(height(T));
medium
A. 4
B. 5
C. 3
D. 6
Solution
Step 1: Understand the readtable range and header
The range 'A1:C5' includes 5 rows, but the first row is usually the header.
Step 2: Calculate the number of data rows
Since the first row is header, the data rows are 5 - 1 = 4.
Final Answer:
4 -> Option A
Quick Check:
Rows read minus header = 4 [OK]
Hint: Remember first row is header, so data rows = total rows - 1 [OK]
Common Mistakes:
Counting header row as data
Confusing range size with number of rows
Assuming no header row present
4. Identify the error in this MATLAB code snippet that tries to write a table T to the second sheet of results.xlsx:
A. The 'Sheet' parameter must be a string, not a number
B. The 'Range' parameter cannot be used with writetable
C. The filename must include full path
D. writetable cannot write to Excel files
Solution
Step 1: Check the 'Sheet' parameter type
In writetable, the 'Sheet' argument should be a string with sheet name, not a numeric index.
Step 2: Validate other parameters
'Range' is valid, filename can be relative, and writetable supports Excel files.
Final Answer:
The 'Sheet' parameter must be a string, not a number -> Option A
Quick Check:
Sheet name must be string in writetable [OK]
Hint: Use sheet names as strings, not numbers, in writetable [OK]
Common Mistakes:
Passing sheet number instead of name
Thinking Range is invalid for writetable
Assuming filename needs full path always
5. You want to read only the numeric data from the range B2:D10 in sheet Data of metrics.xlsx into MATLAB as a table. Which code snippet correctly does this and then writes only the first 5 rows back to a new Excel file subset.xlsx?
hard
A. T = readmatrix('metrics.xlsx', 'Sheet', 'Data', 'Range', 'B2:D10');
writetable(T(1:5,:), 'subset.xlsx');
B. T = readtable('metrics.xlsx', 'Sheet', 'Data', 'Range', 'B2:D10');
writetable(T(1:5,:), 'subset.xlsx');
C. T = readtable('metrics.xlsx', 'Sheet', 'Data', 'Range', 'B2:D10');
writetable(T(1:5));
D. T = readmatrix('metrics.xlsx', 'Sheet', 'Data', 'Range', 'B2:D10');
writetable(T, 'subset.xlsx');
Solution
Step 1: Read numeric data as a table
Use readtable with 'Sheet' and 'Range' to get data as a table.
Step 2: Write first 5 rows to new Excel file
Use writetable with T(1:5,:) to write first 5 rows correctly.
Final Answer:
T = readtable('metrics.xlsx', 'Sheet', 'Data', 'Range', 'B2:D10');
writetable(T(1:5,:), 'subset.xlsx'); -> Option B