Load & Save Commands in MATLAB
💾 Load & Save Commands in MATLAB
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)
📌 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
-
.matfiles preserve data type & structure -
savewithout 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
-
savestores workspace data to disk -
loadretrieves saved data -
.matis efficient and preserves structures -
ASCII format enables external compatibility
