Simon
Simon

Reputation: 5039

Matlab - How to save view configuration of matlab neural network

I am trying to configure a neural network using matlab and newff command.

After that, I am trying to visualize my created configuration using the view command.

x = view(net);

How can I save the displayed window to a .png file? I have tried with saveas(x, 'figure.png', 'png') but it won't work? Do you know how can I do that from code?

Upvotes: 3

Views: 4355

Answers (4)

jgeisler
jgeisler

Reputation: 11

At least in R220b this works:

% Create and configure network
net = timedelaynet([1:8], 10);
net= configure(net, {1}, {1});

% This is internally called by view(net) and returns a java object
hv= nnet.internal.nnviz.StandaloneDiagramWidget(net);
% ... that can be used to interact with the graphic
hv.waitUntilDiagramRendered
% ... and get its image data
img= hv.getImageDataForSnapshot;
hv.delete

imwrite(img, 'ftdnn.png')

Upvotes: 0

Jose Medeiros
Jose Medeiros

Reputation: 1

I was triying to get the results of the nntraintool box and create figures of the network configuration, resulting training snapshot, and performance, training state and regression plots.

After seeing the sugestion ,7. of statck overflow I devised the code that goes below to solve these issues. I would apologise for my program style and expect to have contributed for the solution of those issues.

%***************************************************************************
% TrainingToolDisplays
%***************************************************************************

function [] = TrainingToolDisplays(net,P,T) 

%***************************************************************************
% After configuring the net, do a silent net training
%***************************************************************************

net.trainParam.showWindow = 0;
net = train(net,P,T);

%***************************************************************************
% Create a figure for the net configuration
%***************************************************************************

jConfig = view(net);
hConfig = figure('Name','Neural Network Configuration', ...
                 'NumberTitle','off', ...  
                 'Menubar','none', ...
                 'Position',[100 100 600 200], ...
                 'PaperPositionMode', 'auto', ...
                 'Visible','on');

jPanel = get(jConfig,'ContentPane');
[~,h] = javacomponent(jPanel);
set(h, 'units','normalized', 'position',[0 0 1 1])
jConfig.setVisible(false)
jConfig.dispose

%***************************************************************************
% Create a figure for the nntraintool training snap shot
%***************************************************************************

jTrainTool = nntraintool('handle');
hTrainTool = figure('Name','Neural Network Training', ...    
                    'NumberTitle','off', ...  
                    'Menubar','none', ...
                    'Position',[100 100 600 600], ...
                    'PaperPositionMode', 'auto', ...
                    'Visible','on');

jPanel = get(jTrainTool,'ContentPane');
[~,h] = javacomponent(jPanel);
set(h, 'units','normalized', 'position',[0 0 1 1])
jTrainTool.setVisible(false)
jTrainTool.dispose

%***************************************************************************
% Plot the plots you want and get the handles 
%***************************************************************************

nntraintool('plot', 'plotperform');    hPerform = gcf;
nntraintool('plot', 'plottrainstate'); hTrainState = gcf;
nntraintool('plot', 'plotregression'); hRegression = gcf;

%***************************************************************************
% Now you may do whatever you may want with those matlab handles
% hConfig, hTrainTool, hPerform, hTrainState and bRegression
%***************************************************************************

return
%*******************************************************************************

Upvotes: 0

Amro
Amro

Reputation: 124563

The created window is a pure Java figure (not MATLAB Handle Graphics). Try this to capture it:

%# neural net, and view it
net = feedforwardnet(5);
jframe = view(net);

%# create it in a MATLAB figure
hFig = figure('Menubar','none', 'Position',[100 100 565 166]);
jpanel = get(jframe,'ContentPane');
[~,h] = javacomponent(jpanel);
set(h, 'units','normalized', 'position',[0 0 1 1])

%# close java window
jframe.setVisible(false);
jframe.dispose();

%# print to file
set(hFig, 'PaperPositionMode', 'auto')
saveas(hFig, 'out.png')

%# close figure
close(hFig)

Upvotes: 8

mmenvo
mmenvo

Reputation: 67

I also have the same problem, specially when i try to save the Neural network toolbox (nntraintool) generated plots. I use snipping tools to capture those plots. However, please try to use the following one:

Identify the gfx object you need to snap-shoot (its handle). It will come from identifiable properties. Then you can use print option to save it to a file; you need to write the file name, the type; go to this link for more info (http://www.mathworks.com/help/matlab/ref/print.html).

For example, if you want to save the figure with the tag 'performance.fig', you may try:

h = findobj('Type', 'figure', 'tag', 'performance.fig');

    for k = 1:numel(h)

    print(h(k), sprintf('Pic%d.ps',k));

    end;

let me know if this helps, You have to modify codes up to your need. I also got this help from another person in this stackoverflow forum.

Upvotes: 0

Related Questions