fastSaveUpdate - Efficiently update variables in MAT-file
Version 1.1.1 (213 KB) by
Pritesh Parmar
Faster alternative to built-in 'save -append' command, which becomes increasingly slow as the number of variables and size of MAT-file grows
fastSaveUpdate - Efficiently update existing variables in MAT-file
This function provides a faster alternative to MATLAB's built-in
'save -append' command, which can become increasingly slow as the
number of variables and size of a MAT-file grows. The slowdown occurs
because 'save -append' must scan the entire MAT-file to determine
whether each variable exists, and may need to rewrite large portions
of the file if a variable already exists.
Instead of relying on '-append', this function first loads the
variables in the MAT-file that are NOT being updated, then saves
them along with the updated variables. This avoids repeated lookup
and rewriting overhead.
Usage:
fastSaveUpdate(matfile, variablesToSave, progressMSG)
Inputs:
matfile - String specifying the path to the .mat file.
variablesToSave - (Optional) Cell array of variable names (as strings) to
update or add in the MAT-file. If omitted or empty,
all variables in the current workspace will be used.
progressMSG - (Optional) verbose for progress messages [0 for no, 1 for yes]
Example:
x = rand(100);
y = sin(1:100);
z = cos(1:100);
save('myData.mat', 'x', 'y', 'z'); % Initial save
fastSaveUpdate('myData.mat', {'y','z'}, 1); % Update variables y and z efficiently
Notes:
- *** MATLAB's built-in 'save -append' functionality is faster when
appending new variables that do not already exist in the MAT-file.
The current function, fastSaveUpdate, is faster only for updating the
existing variables.
- If a variable listed in 'variablesToSave' already exists in the MAT-file,
it will be overwritten.
- All other existing variables in the file are preserved.
- This function may use more RAM memory than 'save -append', as it temporarily
loads existing variables into RAM memory before re-saving.
- For large files with many variables, this approach is often significantly
faster than using 'save -append'.
See also: save, load, who
-------------------------------------------------------------------------
Author: Pritesh Parmar, PhD
Changelog:
v1.0 - (04/15/2025) Initial version implementing faster variable update via selective loading and saving
v1.1 - (04/16/2025) Minor changes; added performance analysis comparing the current function
versus MATLAB's built-in 'save -append'
-------------------------------------------------------------------------
Cite As
Pritesh Parmar (2025). fastSaveUpdate - Efficiently update variables in MAT-file (https://www.mathworks.com/matlabcentral/fileexchange/180812-fastsaveupdate-efficiently-update-variables-in-mat-file), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Created with
R2024a
Compatible with any release
Platform Compatibility
Windows macOS LinuxTags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.