figures only display properly in first run, then they are all displaying on the same figure and i need to restart MATLAB
52 views (last 30 days)
Show older comments
im trying to plot 5 figures, each figure contains 5 subplot. when i run the code for the first time after opening matlab everything is fine and the data is displayed properly. when i try to rerun the code all the figures are all in one figure and the dispaly data just rewrites itself, meaning only the last sublpot is showed, and i can observe the figures in animation or something. i then need to restart matlab and everything is working fine again. what am i doing wrong?
if any(run_time <= t & run_time >= t-tau)
x1 = linspace(0, L, K+1); % K+1 points to cover the range from 0 to L
x1 = x1(1:end-1) + h/2; % Shift to the center of each interval
% Plot V
figure(1);
subplot(5,1,plot_counter);
bar(x1, V, 'FaceColor', 'red', 'BarWidth', 1);
title(['V-time ', num2str(t)]);
xlabel('Position(mm)');
ylabel('number of molecules');
xlim([0, L]); % Set the x-axis limits
drawnow;
% Plot EV
figure(2);
subplot(5,1,plot_counter);
bar(x1, EV, 'FaceColor', 'cyan', 'BarWidth', 1);
title(['EV-time ', num2str(t)]);
xlabel('Position(mm)');
ylabel('number of molecules');
xlim([0, L]); % Set the x-axis limits
drawnow;
% Plot B
figure(3);
subplot(5,1,plot_counter);
bar(x1, B, 'FaceColor', 'yellow', 'BarWidth', 1);
title(['B-time ', num2str(t)]);
xlabel('Position(mm)');
ylabel('number of molecules');
xlim([0, L]); % Set the x-axis limits
drawnow;
% Plot A
figure(4);
subplot(5,1,plot_counter);
bar(x1, A, 'FaceColor', 'blue', 'BarWidth', 1);
title(['A-time ', num2str(t)]);
xlabel('Position(mm)');
ylabel('number of molecules');
xlim([0, L]); % Set the x-axis limits
drawnow;
% Plot M
figure(5);
subplot(5,1,plot_counter);
bar(x1, M, 'FaceColor', 'green', 'BarWidth', 1);
title(['M-time ', num2str(t)]);
xlabel('Position(mm)');
ylabel('number of molecules');
xlim([0, L]); % Set the x-axis limits
drawnow;
plot_counter = plot_counter+1;
end
2 Comments
Voss
on 17 Apr 2024 at 16:18
Please share the full code. Upload m-file(s) using the paperclip button.
Arun
on 22 Apr 2024 at 7:20
Edited: Arun
on 22 Apr 2024 at 7:25
Hi @Yiftach, I tried to reproduce the problem using dummy values for variable with the below code, but for me it seems to work correctly for multiple runs. If possible share the complete code and the release you are using, if the issue persists.
for plot_counter = 1:5
t = 10 + 5*plot_counter;
run_time =10 + 5*plot_counter;
tau = 0;
L = 20 + 5*plot_counter;
K = 100;
h = 1;
V = linspace(0,L,K);
EV = linspace(0,L,K);
A = linspace(0,L,K);
B = linspace(0,L,K);
M = linspace(0,L,K);
if any(run_time <= t & run_time >= t-tau)
x1 = linspace(0, L, K+1); % K+1 points to cover the range from 0 to L
x1 = x1(1:end-1) + h/2; % Shift to the center of each interval
% Plot V
figure(1);
subplot(5,1,plot_counter);
bar(x1, V, 'FaceColor', 'red', 'BarWidth', 1);
title(['V-time ', num2str(t)]);
xlabel('Position(mm)');
ylabel('number of molecules');
xlim([0, L]); % Set the x-axis limits
drawnow;
% Plot EV
figure(2);
subplot(5,1,plot_counter);
bar(x1, EV, 'FaceColor', 'cyan', 'BarWidth', 1);
title(['EV-time ', num2str(t)]);
xlabel('Position(mm)');
ylabel('number of molecules');
xlim([0, L]); % Set the x-axis limits
drawnow;
% Plot B
figure(3);
subplot(5,1,plot_counter);
bar(x1, B, 'FaceColor', 'yellow', 'BarWidth', 1);
title(['B-time ', num2str(t)]);
xlabel('Position(mm)');
ylabel('number of molecules');
xlim([0, L]); % Set the x-axis limits
drawnow;
% Plot A
figure(4);
subplot(5,1,plot_counter);
bar(x1, A, 'FaceColor', 'blue', 'BarWidth', 1);
title(['A-time ', num2str(t)]);
xlabel('Position(mm)');
ylabel('number of molecules');
xlim([0, L]); % Set the x-axis limits
drawnow;
% Plot M
figure(5);
subplot(5,1,plot_counter);
bar(x1, M, 'FaceColor', 'green', 'BarWidth', 1);
title(['M-time ', num2str(t)]);
xlabel('Position(mm)');
ylabel('number of molecules');
xlim([0, L]); % Set the x-axis limits
drawnow;
end
end
Answers (0)
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!