Tables in MATLAB

MATLAB Tutorial

📋 Tables in MATLAB

Tables in MATLAB are powerful data containers designed to work with column-oriented, heterogeneous data (numbers, strings, categorical, datetime, etc.).

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

Name Age Marks
____ ___ _____
Amit 20 85
Sara 21 90
John 22 78

🔸 Specify Column Names



2️⃣ Accessing Table Data

🔹 Access Column by Name (Most Common)


Output

85
90
78

🔹 Access Rows & Columns (Indexing)


📌 Returns a subtable.


🔹 Access Table Content Using { }


Output

85

📌 Important Difference

SyntaxReturns
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 table

  • Very 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

You may also like...