MATLAB Answers

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

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;
load('Measuring_Data.mat');
axes1 = axes('Parent',figure);
set(gca,'XTick',0:1:size(A,1));
set(gca,'XTickLabel',0:1:size(A,1));
%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',...
'FontSize',16);
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');
ylim([0,100]);
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');
ylim([0,100]);
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');
ylim([0,100])
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');
ylim([0,100]);
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');
ylim([0,100]);
% 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])
end
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.

  0 Comments

Sign in to comment.

Products


Release

R2014a

1 Answer

Answer by Guillaume
on 14 Jun 2019
 Accepted Answer

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);
hold('on');
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]);
end

  3 Comments

Hello Sir,
Thank you for your answer, still I didn't get the answer for the figure.
For an example If I have to plot 25 subplots, so I can't do all 25 subplots in one figure, so figure number should also be increased according to the numbers.
I don't know is it possible or not?
If possible kindly let me know sir.
Thanks in advance.
Of course it is possible. You can do pretty much anything you want. If you want to create a new figure every 6 subplot, you could do something like this:
function my_plot(n)
%... initialisation code
for plotnumber = 1:n
subplotindex = mod(plotnumber - 1, 6); %0-based
if subplotindex == 0
%we're on the first subplot of a figure with 6 subplot. So create and customise a new figure
hfig = figure; %create new figure
%... code to customise the figure
end
subplot(2, 3, subplotindex + 1);
hold('on');
bar(1, x(plotnumber, :), 'FaceColor', [0.94 0.94 0.94], 'LineWidth', 1);
bar(2, Measuring_Data(plotnumber, 1), 'FaceColor', [0.25 0.25 0.25], 'LineWidth', 1);
title(sprintf('K %d', plotnumber));
ylim([0, 100]);
end
end

Sign in to comment.