Load & Save Commands in MATLAB

MATLAB Tutorial

💾 Load & Save Commands in MATLAB

The load and save commands in MATLAB are used to store data to disk and retrieve it later.

They are essential for data persistence, sharing results, backups, and workflow continuity.
MATLAB is developed by MathWorks.


🔹 What Are load and save?

  • save → writes variables from the workspace to a file

  • load → reads variables from a file into the workspace

  • Default file format is MATLAB binary (.mat)


1️⃣ Saving Variables — save

🔸 Save All Workspace Variables


📌 Saves all variables (a, b) into data.mat.


🔸 Save Specific Variables


 

📌 Only x and z are saved.


🔸 Save with a Different File Name



2️⃣ Loading Variables — load

🔸 Load All Variables from a File


📌 Variables appear directly in the workspace.


🔸 Load Specific Variables


📌 Loads only variable x.


🔸 Load into a Structure (Safe Practice)


📌 Prevents overwriting existing variables.


3️⃣ Saving Data in Text Format (ASCII)

🔸 Save as Text File


Result (matrix.txt)

1 2 3
4 5 6

📌 Use when sharing with non-MATLAB tools.


4️⃣ Loading ASCII/Text Data



5️⃣ Appending Data to an Existing MAT File


📌 Adds p without deleting existing variables.


6️⃣ Checking File Contents (Without Loading)


📌 Lists variables stored in the file.


⚠️ Important Notes

  • .mat files preserve data type & structure

  • save without variable names saves everything

  • Use structure loading to avoid name conflicts

  • ASCII format loses complex structures


🎯 Interview Questions: Load & Save Commands

🔹 Q1. What is the default file format used by save?

Answer: .mat (MATLAB binary file)


🔹 Q2. How do you save only selected variables?

Answer:
save('file.mat','var1','var2')


🔹 Q3. How do you append data to an existing file?

Answer:
Using -append option.


🔹 Q4. How do you load data safely without overwriting variables?

Answer:
Load into a structure: S = load('file.mat').


🔹 Q5. Can MATLAB save data as a text file?

Answer:
Yes, using -ascii.


🔹 Q6. How do you check variables inside a MAT file?

Answer:
Using whos('-file','file.mat').


Summary

  • save stores workspace data to disk

  • load retrieves saved data

  • .mat is efficient and preserves structures

  • ASCII format enables external compatibility

You may also like...