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.