CSV & Excel Files in MATLAB
📊 CSV & Excel Files in MATLAB
MATLAB provides easy built-in functions to read and write these files efficiently.
MATLAB is developed by MathWorks.
🔹 Difference Between CSV & Excel Files
| Feature | CSV File | Excel File |
|---|---|---|
| Extension | .csv |
.xlsx, .xls |
| Data Type | Text-based | Spreadsheet |
| Sheets | ❌ No | ✅ Yes |
| Formatting | ❌ No | ✅ Yes |
| Size | Small | Larger |
1️⃣ Reading CSV Files in MATLAB
✅ Using readmatrix() (Numeric Data)
📌 Best for pure numeric data.
✅ Using readtable() (Mixed Data + Headers)
Example Output
📌 Recommended when CSV contains text and column names.
2️⃣ Writing CSV Files in MATLAB
✅ Write Numeric Data
✅ Write Table Data
📌 Automatically includes headers.
3️⃣ Reading Excel Files (.xlsx)
Read_Numeric Data
Read_Table from Excel
Read Specific Sheet
Read Specific Range
4️⃣ Writing Excel Files
Write_Numeric Data
Write_Table to Excel
Write_to Specific Cell
5️⃣ Appending Data to CSV / Excel
📌 Useful for logs and reports.
6️⃣ Common Errors & Solutions
| Problem | Solution |
|---|---|
| File not found | Place file in current folder |
| Mixed data error | Use readtable() |
| Overwriting data | Use 'WriteMode','append' |
| Wrong sheet | Specify 'Sheet' |
🎯 Interview Questions: CSV & Excel Files in MATLAB
🔹 Q1. Which function is best to read CSV files?
Answer: readmatrix() for numeric, readtable() for mixed data.
🔹 Q2. How do you read Excel files in MATLAB?
Answer: Using readmatrix() or readtable().
🔹 Q3. Which function writes data with headers?
Answer: writetable().
🔹 Q4. How do you read a specific Excel sheet?
Answer: Using 'Sheet' option.
🔹 Q5. Difference between CSV and Excel files?
Answer:
CSV → text, single sheet
Excel → formatted, multiple sheets
🔹 Q6. How do you append data to a CSV file?
Answer: Using 'WriteMode','append'.
✅ Summary
-
MATLAB supports CSV & Excel seamlessly
-
readmatrix()→ numeric data -
readtable()→ mixed data + headers -
writetable()→ best for reports -
Essential for real-world data handling
