Sunday, July 30, 2017

[Matlab GUI] Build simple Data Processing tool - 5/5

5. Clear the axes, and Move the figure to new figure (Undock)

After drawing a plot, you can either clear the plot or undock the plot from the axes.

1) Clear
function Clear_ClickedCallback(hObject, eventdata, handles)
% hObject    handle to Clear (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
try
    cla(all,'reset'); % Clear the entire axes and remove the y-axis
catch
    cla();
end
handles.legend = [];
legend off;
set(handles.axes1,'Position',handles.inipos);

2) Undock
function Undock_ClickedCallback(hObject, eventdata, handles)
% hObject    handle to Undock (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

warning('off');

fig_children = get(gcf,'children');
fig_Axes = findall(fig_children,'Type','Axes');
fig_legend = findall(fig_children,'Tag','legend');
fig = figure;

% copy an existing post from gui to new figure window
copyobj(fig_Axes,fig);

% Delete the legends because the size is not well-suited
dleg = findall(fig,'Tag','legend'); delete(dleg);

% Clear an offset in position of an existing post so it can be centered on a new figure window 
set(gca,'ActivePositionProperty','outerposition');
set(gca,'Units','normalized');
set(gca,'OuterPosition',[0 0 1 1]);
set(gca,'position',[0.1200 0.1100 0.790 0.8150]);

% Change the default logo on the new figure appeared.
try 
    imgPath = fullfile(pwd,'itk.jpg');
    javaFrame = get(handle(gcf),'JavaFrame');
    javaFrame.setFigureIcon(javax.swing.ImageIcon(imgPath)); 
catch
    if code == 1
        warndlg('1','error');
    elseif code == 2
        warndlg('2','error')
    end
end
% Set the legend on the new figure
l = legend('show'); set(l,'Interpreter','none');
warning('on');

Related Posts
0. Build simple plot tool
1. Create a tool layout in GUIDE
2. Load mat file in structure array, and Display the file list loaded on the listbox
3. Diplay variables of mat file, and Plot variables on the axes
4. How to add new plot to the existing plot, and Use dynamic legend
5. Clear the axes, and Move the figure to new figure (Undock)

No comments:

Post a Comment