How to generate the n number of figure for n numbers of subplots

40 views (last 30 days)
This is my pseudo-code for my model
function my_plot(n)
A = zeros(n,n);
x(1:1:size(A,1),1) = 293.50;
axes1 = axes('Parent',figure);
%set(gca,'YTick', 100:10:1000);
% Create xlabel
xlabel('Node Points','FontSize',14);
xlim([0 size(A,1)]);
% Create ylabel
ylabel('Temperatur in °C','FontSize',14);
% Create title
title('Stationary Node Temperatures',...
hold on
y = 1:1:size(A,1);
bar(y, x(:),0.4);
hold off
% I am talking about this section :
% I know title should be entered manually, kindly leave that part.
figure('Color',[1 1 1]);
subplot 231
hold on
bar(1,x(1,:),'FaceColor',[0.94 0.94 0.94],'LineWidth',1);
hold on
bar(2,Measuring_Data(1,1),'FaceColor',[0, 0 , 0],'LineWidth',1);
title('K 1');
subplot 232
hold on
bar(1,x(2,:),'FaceColor',[0.94 0.94 0.94],'LineWidth',1);
hold on
bar(2,Measuring_Data(2,1),'FaceColor',[0.25 0.25 0.25],'LineWidth',1);
title('K 2');
subplot 233
hold on
bar(1,x(3,:),'FaceColor',[0.94 0.94 0.94],'LineWidth',1);
hold on
bar(2,Measuring_Data(3,1),'FaceColor',[0.25 0.25 0.25],'LineWidth',1);
title('K 3');
subplot 234
hold on
bar(1,x(4,:),'FaceColor',[0.94 0.94 0.94],'LineWidth',1);
hold on
bar(2,Measuring_Data(4,1),'FaceColor',[0.25 0.25 0.25],'LineWidth',1);
title('K 4');
legend('Simulation','Messung', 'Location', 'Northeast');
subplot 235
hold on
bar(1,x(5,:),'FaceColor',[0.94 0.94 0.94],'LineWidth',1);
hold on
bar(2,Measuring_Data(5,1),'FaceColor',[0.25 0.25 0.25],'LineWidth',1);
title('K 5');
% subplot 236
% hold on
% bar(1,x(6,:),'FaceColor',[0.94 0.94 0.94],'LineWidth',1);
% hold on
% bar(2,Measuring_Data(6,1),'FaceColor',[0.25 0.25 0.25],'LineWidth',1);
% title('K 6');
% ylim([0,100])
Now If I enter my_plot(5) in the command window it will work along with give the subplot figure also.
Now I want to change n to 20 from 5. At that time only first figure will come, second figure with measuring data will not come since n will be more than 5.
Now my question is how to build the code, so subplot will be generated for n number amd also n figure will be generated for n number.
To run the code, write any numbers in measuring data for your understanding purpose. but It should be n*1 matrix. like 23;45;60;54;21;79;.....
I don't know is it possible or not?
If possible kindly give me a hint or code or suggestions.
Thanks in advance.

Accepted Answer

Guillaume on 14 Jun 2019
I was very surprised to find that
subplot 231
works. It's an undocumented feature of subplot, I would strongly recommend against using it. Instead use:
subplot(2, 3, 1)
Each time you duplicate code, you're doing work that the computer can do for you. In this instance, all your subplots can be plotted with a loop. The biggest difficulty with changing the number of subplots is going to find the optimal number of rows and columns on which to spread the subplots. It's up to you to decide how to do that. One possible way:
function my_plot(n)
%choose number of subplot rows and column according to n
nrows = floor(sqrt(n));
ncols = ceil(n/nrows);
Depending on the shape of your plots, this may not be optimal.
You can then use a simple loop to generate the subplot
for subp = 1:n
subplot(nrows, ncols, subp);
bar(1, x(n, :), 'FaceColor', [0.94 0.94 0.94], 'LineWidth', 1);
bar(2, Measuring_Data(n, 1), 'FaceColor', [0.25 0.25 0.25], 'LineWidth', 1);
title(sprintf('K %d', n));
ylim([0, 100]);

Sign in to comment.

More Answers (0)




Community Treasure Hunt

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

Start Hunting!