For my workflow, I am continually updating a set of figures that provide information on the current status of a remotely operated underwater glider. I have these figures arranged on my desktop so I can view all of them at once to assess the glider's status. When I update them, I don't want to create a new figure every time (and end up with a million figures that are nearly identical), so I have assigned each of the specific figure types a number and I just clear them (with clf) before regenerating after each surfacing and communication with the glider.
For my mapping figure, the basemap showing detailed bathymetry is quite large. With my current approach, I have to replot the bathymetry and contour lines each time, which is very slow and memory intensive. I would like to save the bathymetry and contour lines as a "basemap.fig" that I can load more quickly. Then, I can overlay the latest location data on the basemap.
I run this script 2-3 times a day, for two different gliders, for 8 weeks, so inevitably I do have to reboot my computer sometimes in between. I would like to save the basemap as a .fig (rather than just plot it once and hold it as a handle in the workspace) because it can take 30+ minutes to generate the contours each time (and piloting the gliders can be time sensitive so if IT rebooted my computer in the night, I can't always be waiting 30+ minutes to load a map; also, no, I can't get IT to not reboot).
However, if I load basemap.fig using openfig, it creates a NEW figure. This then has the potential to leave me with many many figures, each just slightly different, being created each time I run the processing script. I realize this isn't the end of the world, but I am also creating many other control figures and my desktop gets cluttered very quickly.
So, my question is - Is there a way to open a saved .fig file as a specific figure number?
Here is my minimum reproducible example. Note, this does require the mapping toolbox. I am not sure the issue I'm having is mapping toolbox dependent but because I know the way axes work is a bit different with the mapping toolbox I wanted to use that in this example.
% SET UP
% set my figure number
figNum = 100; % target figure number
% want to continually overwrite this figure, keeping it in this spot, as I
% get updated track data
% CREATE BASEMAP FIGURE
% this is the slow step so I want to make this once, save it, and reload it
% when I want to replot.
baseFig = figure(figNum); % I don't think assigning the number here matters
ax = axesm('mercator', 'MapLatLim', [19.0 22.75], 'MapLonLim', [-160.0 -154.25]);
states = shaperead('usastatehi', 'UseGeoCoords', true);
geoshow(states);
savefig('baseFigFile.fig');
% LOAD BASEMAP AND ADD TO IT
% this is where I want it to consistently be the same figure number
mapFig = figure(figNum);
% open the existing figure that was saved as a .fig file
mapFig = openfig('baseFigFile.fig'); % where baseFig is a fullfile path and filename to the saved .fig
% add some data to the baseFig
plotm([19.4; 20.5], [-156.5; -157.5]);
textm([19.4; 20.5], [-156.5; -157.5], {'LW01'; 'LW02'});
As I have it now, when I load the saved baseMapFile.fig, it just opens as a new figure (sequentially from 1, not replacing any existing figures).
EDIT I realize now this is very much an XY problem and that the solution may not be in opening a .fig file and assigning it to a specific figure number.
So, my revised, broader question is - how can I load a .fig file (containing a basemap that takes a long time to create so needs to be saved) and then plot new data on that same axis, without cluttering my desktop with new, ever so slightly different figures, each time I run it?
You need a paradigm shift to get to the solution you want- its not the figure you want to update, it is the axis in the figure window. In Matlab, the "figure", created by the
figurecommand, is just a window:It is the axis in the figure window where plots are created.
So the axis and the figure are not the same- you could have multiple axes in the same window (the
subplotcommand does this), you could destroy the axis without closing the figure, etc.This can be confusing because the basic
plotcommand does three things for you automatically: creates a figure window, creates an axes in the figure, and draws your data lines on it. That is convenient when you just need a plot, but if you want to do more sophisticated graphics operations, you need to understand the different graphics components.Also, it will be helpful to internalize the concept of graphics handles: a variable that is a reference to an object. So the handle to a figure window is the variable that specifies a figure (not the figure number); the handle to an axis is the way to specify an axis when you want to add a plot to it, etc.
So, to update a complex data plot repeatedly, use a handle to the axis to make updates. At a high level, this looks like:
For example:
Note that all the commands here specify the axis handle (
hax) explicitly. In most examples in the documentation, this is left out, because all of the Matlab functions assume you are working with only one plot at a time.This example uses simple plot operations; the
plotmcommand from the Mapping Toolbox is the equivalent ofplot, so the same concepts apply.Another helpful graphics handle function is
gca, this will return the handle to the current axes. So, if you open a figure file that was saved to disk, and you want to get the axis handle so you can use the techniques discussed above, you can do: