Open figure and work on that figure

Dear all,
I would like to ask it is a way to work on a saved figure. For example, add more line plots and also change the axes. I have been trying but didn't succeed.
Here is the code that I made:
figure
hold on
openfig('x.fig')
plot(A);
ax = gca;
ax.XTick = [0 0.01 0.03 0.05]
Thanks

4 Comments

figure opens a new figure, and then openfig opens the save figure, so then you have two opened. Then you proceed to use plot, but maybe the NextPlot property is not set to add, so unless you use hold('on'), it will overwrite the loaded axes contents.
in other words... rearrange the lines a little so the 'hold on' is after the openfig:
figure
openfig('x.fig')
hold on
plot(A);
ax = gca;
ax.XTick = [0 0.01 0.03 0.05]
You will notice that the call to figure is not needed, as openfig already opens a new figure window for the loaded figure.
You should use the
fig = openfig(...)
syntax too to get an explicit handle for your figure. Relying on your desired figure always being the one in focus can lead to some unexpected bugs.
Working with explicit handles is always better. Obviously in this case that is limited - I assume your figure only contains one axes, otherwise I have no idea which axes in the figure would be regarded as the current one, but if you do only have one axes:
hFig = openfig( ... );
hAxes = hFig.CurrentAxes;
will give you your explicit axes handle to work with as well as the figure handle, if required.

Sign in to comment.

Answers (1)

Hello,
To summarize the discussion in the comments: When modifying a saved MATLAB figure by adding plots and changing axes properties, it's important to explicitly create a handle for your figure. There's no need to call figure beforehand, as openfig already opens a new figure window for the loaded figure.
Below is an example demonstrating how to create a figure, save it, and later modify its axes properties and add a new plot:
% Part 1: Create and save initial figure
x = linspace(0, 2*pi, 100);
y1 = sin(x);
% Create initial figure
fig1 = figure('Name', 'My Plot');
plot(x, y1, 'b-', 'LineWidth', 2);
xlabel('X-axis');
ylabel('Y-axis');
title('Initial Plot');
grid on;
savefig(fig1, 'x.fig');
% Part 2: Load and modify the saved figure
% Open the saved figure and get handles
hFig = openfig('x.fig');
hAxes = hFig.CurrentAxes;
y2 = cos(x);
hold(hAxes, 'on');
plot(hAxes, x, y2, 'r--', 'LineWidth', 2);
% Modify axes properties
hAxes.XTick = 0:pi/2:2*pi;
hAxes.XTickLabel = {'0', '\pi/2', '\pi', '3\pi/2', '2\pi'};
hAxes.YLim = [-1.5 1.5];
legend(hAxes, 'sin(x)', 'cos(x)');
% Save the modified figure if needed
savefig(hFig, 'x_modified.fig');
Note: Working with explicit handles prevents common issues like plotting to wrong axes or losing track of active figures

Categories

Find more on Graphics Performance in Help Center and File Exchange

Tags

Asked:

on 29 Sep 2018

Answered:

on 3 Jan 2025

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!