Excel files store data in tables that are easy to read and edit. Reading and writing Excel files lets you work with this data inside MATLAB.
Excel file reading and writing in MATLAB
data = readtable('filename.xlsx'); writetable(data, 'filename.xlsx');
readtable reads Excel data into a table format in MATLAB.
writetable writes a MATLAB table back to an Excel file.
data.data = readtable('data.xlsx');data into a new Excel file named 'output.xlsx'.writetable(data, 'output.xlsx');data = readtable('data.xlsx', 'Sheet', 'Sheet2');
data to the sheet named 'Results' in the Excel file.writetable(data, 'output.xlsx', 'Sheet', 'Results');
This program creates a small table with names and scores, writes it to an Excel file named 'sample.xlsx', then reads the data back and displays it.
filename = 'sample.xlsx'; % Create sample data Names = {'Anna'; 'Ben'; 'Cara'}; Scores = [85; 92; 78]; T = table(Names, Scores); % Write data to Excel file writetable(T, filename); % Read data back from Excel file readData = readtable(filename); disp('Data read from Excel file:'); disp(readData);
Make sure the Excel file is not open in another program when writing to it, or MATLAB may give an error.
You can specify sheet names and ranges to read or write specific parts of the Excel file.
Tables are a convenient way to handle Excel data because they keep column names and data together.
Use readtable to load Excel data into MATLAB as a table.
Use writetable to save MATLAB tables back to Excel files.
You can specify sheets and ranges to control what part of the Excel file you read or write.
Practice
Solution
Step 1: Identify the function for reading Excel files
MATLAB providesreadtableto import Excel data as a table.Step 2: Differentiate from other functions
xlswritewrites data,csvreadreads CSV files, andloadloads MAT files.Final Answer:
readtable -> Option CQuick Check:
Excel read function = readtable [OK]
- Confusing readtable with xlswrite
- Using csvread for Excel files
- Trying to use load for Excel data
T to an Excel file named data.xlsx?Solution
Step 1: Identify the correct function for writing tables
The functionwritetablewrites tables to Excel files.Step 2: Check the syntax
The syntax iswritetable(T, 'filename.xlsx'). Other options are either reading functions or incorrect.Final Answer:
writetable(T, 'data.xlsx') -> Option DQuick Check:
Write table syntax = writetable(T, filename) [OK]
- Using write instead of writetable
- Confusing xlsread with writing
- Passing table as second argument to readtable
T = readtable('sales.xlsx', 'Sheet', 'Q1', 'Range', 'A1:C5');
disp(height(T));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 AQuick Check:
Rows read minus header = 4 [OK]
- Counting header row as data
- Confusing range size with number of rows
- Assuming no header row present
T to the second sheet of results.xlsx:writetable(T, 'results.xlsx', 'Sheet', 2, 'Range', 'B2');
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 AQuick Check:
Sheet name must be string in writetable [OK]
- Passing sheet number instead of name
- Thinking Range is invalid for writetable
- Assuming filename needs full path always
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?Solution
Step 1: Read numeric data as a table
Usereadtablewith 'Sheet' and 'Range' to get data as a table.Step 2: Write first 5 rows to new Excel file
UsewritetablewithT(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 BQuick Check:
readtable + subset rows + writetable = T = readtable('metrics.xlsx', 'Sheet', 'Data', 'Range', 'B2:D10'); writetable(T(1:5,:), 'subset.xlsx'); [OK]
- Using readmatrix but then writetable on matrix
- Omitting filename in writetable
- Not slicing rows correctly before writing
Ready to prove it?
Pass the test and this pattern moves unknown
