Keep existing subplots while running code multiple times

Hello,
I have written a code which calculates certain parameters and plots several subplots in a figure as follows:
figure('name','my_title');
h1=subplot(2,2,1);
plot (x1,y1);
h1=subplot(2,2,2);
plot(x2,y2);
h1=subplot(2,2,3);
plot(x3,y3);
h1=subplot(2,2,4);
plot(x4,y4);
Now I have to run the same code multile times for differnet data sets. I would like to keep the existing subplots (from lets say data set 1) and plot the new subplots (from lets say data set 2) on top of the existing subplots for comparison. But everytime I try to do it, its erasing the existing subplots and only plotting the current data set.
I tried to use findobj to check whether the figure exists and if so then use it as follows:
fOp.figName='my_title';
hf=findobj('Type','figure');
if ~isempty(hf)
figure(hf);hold all;
else
hf = figure('Name',fOp.figName);
hold all;
end
h1=subplot(2,2,1);
hold all;
plot (x1,y1);
h1=subplot(2,2,2);
hold all;
plot(x2,y2);
h1=subplot(2,2,3);
hold all;
plot(x3,y3);
h1=subplot(2,2,4);
hold all;
plot(x4,y4);
While it keeps the figure 1 open for the multiple runs, but it still erases the previous subplots inside the figure 1. I think I have to use 'Children' function in some way but not sure how. Any help would be much appreciated. Thank you.

3 Comments

your reassignment h1 = subplot(2,2,1) is overwriting your previous subplot
if you keep your original assignment h1, you don't need to find the figure and then the axes.
just do hold(h1,'on')
then plot(h1,...)
and so on.
Thanks for the answer. I tried hold on. It didnt work. I think the problem is since I am running the code multiple times, I need a handle for the subplot just like figure.
@Mohammad you should post your comment as an answer. When doing so you should probably elaborate on your suggestion of using an explicit axes handle in the plot call.

Sign in to comment.

 Accepted Answer

You can change your code as follows.
fOp.figName='my_title';
hf = findobj('Type','figure','Name',fOp.figName);
if ~isempty(hf)
% get the handles if the figure already exist
hf = figure(hf);
h1 = hf.Children(4); % subplot 221
h2 = hf.Children(3); % subplot 222
h3 = hf.Children(2); % subplot 223
h4 = hf.Children(1); % subplot 224
else
% create figure and subplots if the figure does not exist
hf = figure('Name',fOp.figName);
h1=subplot(2,2,1);
h2=subplot(2,2,2);
h3=subplot(2,2,3);
h4=subplot(2,2,4);
end
hold(h1,'on');
plot(h1,x1,y1);
hold(h2,'on');
plot(h2,x2,y2);
hold(h3,'on');
plot(h3,x3,y3);
hold(h4,'on');
plot(h4,x4,y4);

6 Comments

Hi Mohammad,
Thanks for the answer. I tried your solution, but seems like it does not plot the first three subplots even in the first run of the code (with the first data set) and it plotting subplots 223 and 224 together (red and blue in the attached figure). I attached how the plot looks like. I have no idea why it is doing this.
It seems to be working for me. How are you running your code. Perhaps, it would be better to convert into a function. Can you try this test script
% first run
x1 = rand(100,1);
y1 = rand(100,1);
x2 = rand(100,1);
y2 = rand(100,1);
x3 = rand(100,1);
y3 = rand(100,1);
x4 = rand(100,1);
y4 = rand(100,1);
fOp.figName='my_title';
hf = findobj('Type','figure','Name',fOp.figName);
if ~isempty(hf)
% get the handles if the figure already exist
hf = figure(hf);
h1 = hf.Children(4); % subplot 221
h2 = hf.Children(3); % subplot 222
h3 = hf.Children(2); % subplot 223
h4 = hf.Children(1); % subplot 224
else
% create figure and subplots if the figure does not exist
hf = figure('Name',fOp.figName);
h1=subplot(2,2,1);
h2=subplot(2,2,2);
h3=subplot(2,2,3);
h4=subplot(2,2,4);
end
hold(h1,'on');
plot(h1,x1,y1);
hold(h2,'on');
plot(h2,x2,y2);
hold(h3,'on');
plot(h3,x3,y3);
hold(h4,'on');
plot(h4,x4,y4);
% second run
x1 = rand(100,1);
y1 = rand(100,1);
x2 = rand(100,1);
y2 = rand(100,1);
x3 = rand(100,1);
y3 = rand(100,1);
x4 = rand(100,1);
y4 = rand(100,1);
fOp.figName='my_title';
hf = findobj('Type','figure','Name',fOp.figName);
if ~isempty(hf)
% get the handles if the figure already exist
hf = figure(hf);
h1 = hf.Children(4); % subplot 221
h2 = hf.Children(3); % subplot 222
h3 = hf.Children(2); % subplot 223
h4 = hf.Children(1); % subplot 224
else
% create figure and subplots if the figure does not exist
hf = figure('Name',fOp.figName);
h1=subplot(2,2,1);
h2=subplot(2,2,2);
h3=subplot(2,2,3);
h4=subplot(2,2,4);
end
hold(h1,'on');
plot(h1,x1,y1);
hold(h2,'on');
plot(h2,x2,y2);
hold(h3,'on');
plot(h3,x3,y3);
hold(h4,'on');
plot(h4,x4,y4);
Thank you Mohammad. I misunderstood the part of the code and I now copied and paste your code and it works. Thank you very much. A quick question: if I have to do this for multiple figures ( like figure 1 (with 4 subplots), figure 2 (with 4 subplots) and so on) , do I just have to use a for loop?
I would suggest, convert this into a function. That way we can be more explicit about which figure you are plotting on during the for loop.
function [hf,h1,h2,h3,h4] = myplot(x1,y1,x2,y2,x3,y3,x4,y4,fOp,hf,h1,h2,h3,h4)
assert(ismember(nargin,[8 14]),'Incorrect number of arguments to this function');
if nargin <= 8 || isempty(hf)
% only x and y variables given create a new figure
hf = figure('Name',fOp.figName);
h1=subplot(2,2,1);
h2=subplot(2,2,2);
h3=subplot(2,2,3);
h4=subplot(2,2,4);
end
asse
hold(h1,'on');
plot(h1,x1,y1);
hold(h2,'on');
plot(h2,x2,y2);
hold(h3,'on');
plot(h3,x3,y3);
hold(h4,'on');
plot(h4,x4,y4);
end
You can then call this function in your for loop
num_figs; % your code here
for i = 1:num_figs;
hf = []; h1 = []; h2 = []; h3 = []; h4 = [];
fOp.figName= sprintf('my_title %i',i);
num_overlay; % your code here
for j = 1:num_overlay
% your x,y code here
[hf,h1,h2,h3,h4] = myplot(x1,y1,x2,y2,x3,y3,x4,y4,fOp,hf,h1,h2,h3,h4);
end
end
Thanks Mohammad. I am currently using the code with out creating a funtion. It works great. Only problem I have is to add the legend in the subplot after each run of the code. For example I would like to add legend 'data1' after the first run and then add legend 'data2' next to first legend and so on. I tried to add the legend option after the plot like:
hold(h1,'on');
plot(h1,x1,y1);
legend(h1,'legendText')% I change the legendtext manually each time before running the code
while it works for a single run, but give me following error in the next run:
Error using hold (line 83)
Unknown command option.
Error in test8 (line 891)
hold(h1,'on');
Do you know where/how I can add the legend in the code that you suggested? Thanks!
You can specify the legend in the plot command lgdtxt = 'data1'; plot(h1,x1,y1,'DisplayName',lgdtxt);
Then finally at the end just call the legend. It will use the display name property to create the legends

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products

Release

R2018b

Community Treasure Hunt

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

Start Hunting!