Snap: A Tool for Snapshotting Figures in MATLAB
I can’t tell you how many times I have shared a figure from MATLAB using beta-level code and then forgot what file generated it, let alone, the state of the code. In my mind, there is a gold-standard method of tracking code (and potentially figures) which would be using Git. However, Git workflows are different for everyone; I don’t necessarily want a new commit for each figure I want to share with a buddy on Slack.
My ideal system was more along the lines of a central figure and code repository where prototypes could be saved and potentially forgotten about, but importantly, looked up (or backtracked) way in the future. This requires that figures are saved with a unique filename that not only appears in the figure itself but has a corresponding m-file somewhere, too.
Snap is a simple function that will recognize the caller’s location (i.e. the function file name), embed that into the figure with a unique timestamp, and then archive the current state of the code (creating a ‘snapshot’).
How to Use Snap
In some file, you are generating a figure:
h=figure; plot(rand(10));
Download the snap function (or repo to use the .gitignore) and then snap the figure in your function giving it a unique name:
snap(h,'SnapTest');
Within the snap directory, it will create folders for code
and figs
, where everything is archived. After running, it will open the snapshot figure location (which can be turned off using showFile
). If you snap from the command line, snap will not know the origin function and simply use snap.m
as the originating code. I ran my example from a file called run_this.m
with the following results.
In sum, with one line of code I now have enough forensic power to track down figures, when they were made, and what the code looked like at the time without adding files or requiring additional commits to the code I am working on. I think of this as a replacement to the saveas
function in MATLAB. If anything, some of the tricks within the snap function might be useful if you’re trying to accomplish a similar goal.
Recent Comments
Archives
- April 2023
- January 2023
- November 2022
- May 2022
- March 2022
- January 2022
- December 2021
- April 2021
- December 2020
- October 2020
- August 2020
- July 2020
- March 2020
- February 2020
- January 2020
- December 2019
- November 2019
- October 2019
- January 2019
- December 2018
- November 2018
- August 2018
- July 2018
- April 2018
- March 2018
- November 2017
- October 2017
- February 2017
- October 2016
- August 2016
- July 2016
- November 2015
- October 2013
- February 2013
- January 2013
- August 2012
- July 2012
- June 2012
- May 2012
- April 2012
- February 2012
- December 2011