Post Image
By Matt GaidicaJanuary 26, 2022In Uncategorized

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.

Snap resulting directory structure. The code from run_this.m is saved in the code folder.

Snap figure saved as a PNG. The timestamp, unique name, and originating code is embedded into the figure image (top-left).

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.

svgA Dynamic Hindlimb Unloading Apparatus
svg
svgMemorizing VFR Minimums in 6 Steps: 3-152 BEG Method