Tables in MATLAB

📋 Tables in MATLAB
They are ideal for real-world datasets like Excel/CSV files.
MATLAB is developed by MathWorks.
🔹 What Is a Table in MATLAB?
A table:
Stores data in rows and named columns
Each column can have a different data type
Similar to a spreadsheet or database table
📌 Think of a table as an Excel sheet inside MATLAB.
1️⃣ Creating a Table
🔸 From Workspace Variables
Output
🔸 Specify Column Names
2️⃣ Accessing Table Data
🔹 Access Column by Name (Most Common)
Output
🔹 Access Rows & Columns (Indexing)
📌 Returns a subtable.
🔹 Access Table Content Using { }
Output
📌 Important Difference
| Syntax | Returns |
|---|---|
T() | Table |
T{} | Data inside table |
3️⃣ Adding & Removing Columns
🔸 Add New Column
🔸 Remove Column
4️⃣ Sorting & Filtering Tables
🔸 Sort Rows
🔸 Filter Rows (Logical Indexing)
5️⃣ Table Size & Info
6️⃣ Reading Data into Tables
🔸 Read CSV / Excel
📌 Automatically detects headers & data types.
7️⃣ Writing Tables to Files
8️⃣ Convert Between Table & Other Types
🔸 Table → Structure
🔸 Structure → Table
⚠️ Important Notes
Tables are column-oriented
Use
readtable()for real-world data{}extracts raw data,()keeps tableVery useful for data analysis & reporting
🎯 Interview Questions: Tables in MATLAB
🔹 Q1. What is a table in MATLAB?
Answer:
A data type that stores heterogeneous data in rows and named columns.
🔹 Q2. Difference between cell array and table?
Answer:
Tables have named columns and better data handling.
🔹 Q3. Which function reads Excel/CSV into a table?
Answer: readtable().
🔹 Q4. Difference between T() and T{}?
Answer:T() returns a table, T{} returns actual data.
🔹 Q5. How do you add a new column to a table?
Answer:T.NewColumn = values.
🔹 Q6. How do you filter rows in a table?
Answer:
Using logical indexing: T(T.Col > value,:).
✅ Summary
Tables handle real-world structured data
Support mixed data types
Easy import/export with CSV & Excel
Essential for data analysis projects
