Matlab subplots in loop: only shows plots in last iteration

梦想与她 提交于 2019-12-31 05:39:26

问题


I am trying to create a big plot in Matlab by adding subplots in a loop.

% Generation of examples and targets
x = 0 : 0.05 : 3 * pi;
y = sin(x.^2);

% Deep Learning Toolbox™ software arranges concurrent vectors with a
% matrix, and sequential vectors with a cell array (where the second index is the time step).
% con2seq and seq2con allow concurrent vectors to be converted to sequential vectors, and back again.

p = con2seq(x);
t = con2seq(y); % convert the data to a useful format

% Creation of networks (based on algorlm1.m)
num_hid = 50;

% Epoch quantities to evaluate
num_epochs = [1 20 1000]

% We'll evaluate these algorithms
training_algorithms = {
    'traingd',
    'traingda',
    'traincgf',
    'traincgp',
    'trainlm',
    'trainbfg'
};

%% Initialization 

% Create arrays to store networks and training parameters
networks = cell(length(training_algorithms), 1);

durations = zeros(length(training_algorithms), 1);
slopes = cell(length(training_algorithms), 1);
intercepts = cell(length(training_algorithms), 1);
correlations = cell(length(training_algorithms), 1);

for tai = 1:length(training_algorithms)

   % Create new feedforwardnet 
   net = feedforwardnet(num_hid, training_algorithms{tai});

   % Set all networks weights and biases equal (to the first one created)
   if tai > 1
        net.IW{1,1} = networks{tai-1}.IW{1,1};
        net.LW{1,1} = networks{tai-1}.LW{1,1};
        net.b{1} = networks{tai-1}.b{1};
        net.b{2} = networks{tai-1}.b{2};
   end

   % Store network
   networks{tai} = net;

end

figure;
for tai = 1:6

   net = networks{tai}; % Load network
   net.trainParam.showWindow = false; % Don't show graph

   % Train network, and time training
   tic;
   net = train(net, p, t);
   durations(tai)=toc;

   % Simulate input on trained networks (and convert to double format)
   y_result = cell2mat(sim(net, p));

   % Evaluate result
   [slopes{tai}, intercepts{tai}, correlations{tai}] = postreg(y_result, y);

   % Add network to array
   networks{tai} = net;

   % Plot results
   subplot(2,6,tai);
   plot(x,y,'bx',x,y_result,'r'); % Plot the sine function and the output of the network
   %title('1 epoch');
   legend('target',training_algorithms{tai},'Location','north');
   subplot(2,6, tai+6);
   postregm(y_result, y); % perform a linear regression analysis and plot the result

end

I only get plots that were created in the last iteration though (when tai == 6). I tried adding 'hold on' in front of the loop, and turned it off again. Any ideas why this is happening?

EDIT: Here's the resulting figure:

EDIT2: I added code so it could be reproduced. You'll need the deep learning toolbox.

来源:https://stackoverflow.com/questions/57458639/matlab-subplots-in-loop-only-shows-plots-in-last-iteration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!