0
0
MATLABdata~10 mins

MAT file save and load in MATLAB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - MAT file save and load
Create variables in workspace
Use save('file.mat', 'var1', 'var2', ...)
MAT file 'file.mat' created with variables
Clear or close workspace
Use load('file.mat')
Variables restored in workspace
First, variables are saved into a MAT file. Later, loading the MAT file restores those variables back into the workspace.
Execution Sample
MATLAB
a = 10;
b = [1, 2, 3];
save('data.mat', 'a', 'b');
clear;
load('data.mat');
This code saves variables 'a' and 'b' into 'data.mat', clears the workspace, then loads them back.
Execution Table
StepActionVariables in WorkspaceFile Created/LoadedNotes
1Assign a=10a=10NoneVariable 'a' created
2Assign b=[1,2,3]a=10, b=[1 2 3]NoneVariable 'b' created
3save('data.mat', 'a', 'b')a=10, b=[1 2 3]'data.mat' createdVariables saved to file
4clearNone'data.mat' existsWorkspace cleared
5load('data.mat')a=10, b=[1 2 3]'data.mat' loadedVariables restored
💡 All variables saved and loaded successfully; workspace restored.
Variable Tracker
VariableStartAfter SaveAfter ClearAfter Load
aundefined10undefined10
bundefined[1 2 3]undefined[1 2 3]
Key Moments - 2 Insights
Why do variables disappear after 'clear' but come back after 'load'?
The 'clear' command removes variables from the workspace (see step 4 in execution_table). The 'load' command reads the saved MAT file and restores those variables (step 5).
What happens if you save without specifying variable names?
If you use save('file.mat') without variable names, MATLAB saves all workspace variables at that moment.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What does the 'save' command do?
ACreates a MAT file with variables 'a' and 'b'
BDeletes variables 'a' and 'b'
CLoads variables from 'data.mat'
DClears the workspace
💡 Hint
Check the 'File Created/Loaded' and 'Notes' columns at step 3 in execution_table.
At which step do variables 'a' and 'b' get removed from the workspace?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Variables in Workspace' column in execution_table for step 4.
If you skip the 'clear' command, what will be the workspace state after loading 'data.mat'?
AVariables 'a' and 'b' will be duplicated
BVariables 'a' and 'b' will be overwritten with saved values
CLoading will fail because variables already exist
DWorkspace will be empty
💡 Hint
Consider how 'load' works when variables already exist in the workspace.
Concept Snapshot
MAT file save and load in MATLAB:
- Use save('file.mat', 'var1', 'var2') to save variables.
- Use load('file.mat') to restore variables.
- 'clear' removes variables from workspace.
- Loading restores saved variables.
- Saving without variable names saves all workspace variables.
Full Transcript
This visual execution shows how MATLAB saves variables into a MAT file and loads them back. First, variables 'a' and 'b' are created. Then, the save command writes them into 'data.mat'. After clearing the workspace, the load command restores these variables. The variable tracker shows how 'a' and 'b' exist before saving, disappear after clearing, and come back after loading. Key moments clarify why variables vanish after clear and how save/load work. The quiz tests understanding of each step in the process.