Trying to plot two graphs in Live with a for loop, the second graph overwrites the first graph, Cannot produce 2 separate graphs.

3 views (last 30 days)
I have a 2 data blocks of 15 lines. From each data block I want to plot 5 lines using 'hold'. The code plots the first graph properly, then overwrites the forst graph with the second graph. I have tried every configuration of using figure handles as shown on the help files but none prevent the overwrite. I have attached the code for plottinf two graphs, each base on a 15x9 array. Can anyone point out my error and/or suggest a method to have both graphs appear? I did this in Live/2023b. Any help appreciated!
% Plotting two Separate graphs
clear;clc;
L1= 2.0; dL = 0.5; L2 = 2.5; nL=(L2-L1)/dL+1; % two L values column 2 in data array
r1= 0.8; dr = 0.1; r2 = 1.2; nr=round((r2-r1)/dr+1);% five r values column 3
t1= 0.2; dt = 0.1; t2 = 0.4; nt=(t2-t1)/dt+1; % three t values column 4
% No of array elements
nrowstotal= nL*nr*nt; % total rows in data array
nrowsblk = r*nt; % number of rows of data in each block to be plotted
nblks = nL; % number of blocks of data (2 as shown in dat() )
% Two data blocks, 1-15 and 16-30
% separate graph for each
dat=[
1 2.00 0.80 0.200 0.303 0.642 0.493 0.149 3922
2 2.00 0.80 0.300 0.151 0.790 0.686 0.104 2865
3 2.00 0.80 0.400 0.079 0.911 0.844 0.066 2102
4 2.00 0.90 0.200 0.361 0.766 0.563 0.203 4227
5 2.00 0.90 0.300 0.189 0.941 0.792 0.149 3173
6 2.00 0.90 0.400 0.105 1.089 0.985 0.104 2417
7 2.00 1.00 0.200 0.419 0.899 0.633 0.265 4500
8 2.00 1.00 0.300 0.226 1.100 0.897 0.203 3448
9 2.00 1.00 0.400 0.133 1.275 1.126 0.149 2696
10 2.00 1.10 0.200 0.477 1.040 0.704 0.336 4745
11 2.00 1.10 0.300 0.265 1.268 1.003 0.265 3696
12 2.00 1.10 0.400 0.160 1.470 1.267 0.203 2946
13 2.00 1.20 0.200 0.536 1.189 0.774 0.415 4969
14 2.00 1.20 0.300 0.303 1.444 1.108 0.336 3922
15 2.00 1.20 0.400 0.189 1.673 1.407 0.265 3173
16 2.50 0.80 0.200 0.303 0.802 0.616 0.187 3922
17 2.50 0.80 0.300 0.151 0.987 0.858 0.130 2865
18 2.50 0.80 0.400 0.079 1.139 1.056 0.083 2102
19 2.50 0.90 0.200 0.361 0.958 0.704 0.254 4227
20 2.50 0.90 0.300 0.189 1.176 0.990 0.187 3173
21 2.50 0.90 0.400 0.105 1.361 1.232 0.130 2417
22 2.50 1.00 0.200 0.419 1.123 0.792 0.332 4500
23 2.50 1.00 0.300 0.226 1.376 1.122 0.254 3448
24 2.50 1.00 0.400 0.133 1.594 1.407 0.187 2696
25 2.50 1.10 0.200 0.477 1.300 0.880 0.420 4745
26 2.50 1.10 0.300 0.265 1.585 1.253 0.332 3696
27 2.50 1.10 0.400 0.160 1.837 1.583 0.254 2946
28 2.50 1.20 0.200 0.536 1.486 0.968 0.518 4969
29 2.50 1.20 0.300 0.303 1.805 1.385 0.420 3922
30 2.50 1.20 0.400 0.189 2.091 1.759 0.332 3173];
%% Plot Data
% nt = no pf rows to be plotted
% No of array elements
ntotal= nL*nr*nt; % total number of rows in data array
nblk = 2; % two separate sets of data in the array
s=1; % Start at line 1 for first data set
for blk=1:1:nblk % first data blk of 15 lines
bL = s; % 1st line of data set
bd = nr*nt; % increment
bU = bL+bd-1; % last line of data set
a = dat(bL:bU,:); % create data set to be plotted
ntotal = bU; % total number of data lines in a set
%% Start plot loop
for k=1:nt:bd % plot lines
k1=k; % start data row
k2=k1+nt-1; % end data row
figure(blk); % ID plot
plot(a(k1:k2,4),a(k1:k2,6))
xlabel('tw')
ylabel('Total Mass')
title(['Length: ' num2str(a(1,2)) ' in'])
grid;
hold on % hold plot open for additional lines
end % next line on graph
hold off
% Next block of data
s=s+nt*nr; % Jump to line 16 for 2nd data set
end % Next figure
  1 Comment
Star Strider
Star Strider on 1 Jan 2025
@Randall Lilko — You indicated that there was apparently nothing with your code to begin with, after I ran it and couldn’t detect any errors, and so asked what the problem was

Sign in to comment.

Accepted Answer

Arjun
Arjun on 31 Dec 2024
I see that you are trying to produce two separate graphs but somehow your second graph is overwriting the first one.
I do not really see any issue with the code that is leading to this overwriting.
However, after running multiple times I discovered that sometimes previous figures are not properly closed in MATLAB which can cause issues with the new figures.
Kindly run this command before running the code:
% Close all the figures
close all;
This command will forcefully close all the existing figures and then your code should run fine.
I hope this helps!

More Answers (0)

Categories

Find more on Data Type Identification in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!