Code covered by the BSD License  

Highlights from
spec file reader

image thumbnail
from spec file reader by Zhang Jiang
Monitor, read, save scans from SPEC files and calculate true reflectivity.

print2figure(varargin)
function print2figure(varargin)
% PRINT2FIGURE Called by specr and trueref to print current plot to another
%   new figure or existing figure.
%
% Copyright 2004, Zhang Jiang

hCurrentFig = gcf;
hAxes = gca;

% --- find handles of current figures with integer handles
hFigs = findall(0,'Type','Figure');
hFigs = sort(hFigs(find(hFigs == round(hFigs))));
hFigsCell = {};
for iFigs = 1:length(hFigs)
    if isempty(get(hFigs(iFigs),'name'))
        hFigsCell{iFigs} = num2str(hFigs(iFigs));
    else
        hFigsCell{iFigs} = [num2str(hFigs(iFigs)),' : ',get(hFigs(iFigs),'name')];
    end
end

% --- figure layout for print2figure request
figSize = get(hCurrentFig,'position');
figPrint2figureSize = [figSize(1)+5,(figSize(2)+figSize(4))-170,240,170];
hFigPrint2figure = figure(...
    'Units','pixels',...
    'MenuBar','none',...
    'Units','pixels',...
    'Name','Print to Figure',...
    'NumberTitle','off',...
    'IntegerHandle','off',...
    'Position',figPrint2figureSize,...
    'HandleVisibility','callback',...
    'Tag','figPrint2figure',...
    'Resize','off',...
    'WindowStyle','modal',...
    'UserData',[]);
panelSize = [10 60 figPrint2figureSize(3)-20 figPrint2figureSize(4)-70];
hPanel = uipanel(...
    'Parent',hFigPrint2figure,...
    'BackgroundColor',get(hFigPrint2figure,'Color'),...
    'Title','Print current plot to',...
    'Units','pixels',...
    'Position',panelSize);
hRadiobutton1 = uicontrol(...
    'Parent',hPanel,...
    'Style','radiobutton',...
    'BackgroundColor',get(hFigPrint2figure,'Color'),...
    'Position',[10 panelSize(4)-45 panelSize(3)/2 25],...
    'String','New figure',...
    'Tag','print2figure_radiobuttion1',...
    'Value',1,...
    'callback',@radiobutton1Fcn);
hRadiobutton2 = uicontrol(...
    'Parent',hPanel,...
    'Style','radiobutton',...
    'BackgroundColor',get(hFigPrint2figure,'Color'),...
    'Position',[10 panelSize(4)-80 panelSize(3)/3 25],...
    'String','Figure',...
    'Tag','print2figure_radiobuttion2',...
    'Value',0,...
    'callback',@radiobutton2Fcn);
hPopupmenu = uicontrol(...
    'Parent',hPanel,...
    'Style','popupmenu',...
    'BackgroundColor','w',...
    'Units','pixels',...
    'Position',[panelSize(3)/3+10 panelSize(4)-80 panelSize(3)*2/3-20 25],...
    'String','No figure',...
    'HorizontalAlignment','left',...
    'Enable','off',...
    'Value',1,...
    'Tag','print2figure_popupmenu');
if isempty(hFigs)
    set(hRadiobutton2,'Enable','off');
    set(hPopupmenu,'Enable','off');
else
    set(hPopupmenu,'String',hFigsCell);
end

hPushbuttonOK = uicontrol(...
    'Parent',hFigPrint2figure,...
    'Style','pushbutton',...
    'String','OK',...
    'Position',[20 20 (figPrint2figureSize(3)-60)/2 25],...
    'callback',{@print2figure_OKRequestFcn,hCurrentFig,hAxes});
hPushbuttonCancel = uicontrol(...
    'Parent',hFigPrint2figure,...
    'Style','pushbutton',...
    'String','Cancel',...
    'Position',[figPrint2figureSize(3)/2+10 20 (figPrint2figureSize(3)-60)/2 25],...
    'Callback',@print2figure_CloseRequestFcn);


%================================================================
% --- radiobutton1 callback fcn (export to new figure)
%================================================================
function radiobutton1Fcn(hObject,eventdata)
hFigPrint2figure = gcf;
hRadiobutton1 = findall(hFigPrint2figure,'Tag','print2figure_radiobuttion1');
hRadiobutton2 = findall(hFigPrint2figure,'Tag','print2figure_radiobuttion2');
hPopupmenu = findall(hFigPrint2figure,'Tag','print2figure_popupmenu');
set(hRadiobutton1,'Value',1);
set(hRadiobutton2,'Value',0);
set(hPopupmenu,'Enable','off');


%================================================================
% --- radiobutton1 callback fcn (export to old figure)
%================================================================
function radiobutton2Fcn(hObject,eventdata)
hFigPrint2figure = gcf;
hRadiobutton1 = findall(hFigPrint2figure,'Tag','print2figure_radiobuttion1');
hRadiobutton2 = findall(hFigPrint2figure,'Tag','print2figure_radiobuttion2');
hPopupmenu = findall(hFigPrint2figure,'Tag','print2figure_popupmenu');
set(hRadiobutton1,'Value',0);
set(hRadiobutton2,'Value',1);
set(hPopupmenu,'Enable','on');


%================================================================
% --- print2figure close function
%================================================================
function print2figure_CloseRequestFcn(hObject,eventdata)
delete(gcf);
return;


%================================================================
% --- print to figure
%================================================================
function print2figure_OKRequestFcn(hObject,eventdata,hCurrentFig,hAxes)
% --- get handles of print2figure
hFigPrint2figure = gcf;
hRadiobutton1 = findall(hFigPrint2figure,'Tag','print2figure_radiobuttion1');
hRadiobutton2 = findall(hFigPrint2figure,'Tag','print2figure_radiobuttion2');
hPopupmenu = findall(hFigPrint2figure,'Tag','print2figure_popupmenu');
% --- get handles of lines in main figure with plots
hLine = findall(hAxes,'Type','line');
% --- determine whether to open a new or existing figure
if get(hRadiobutton1,'Value') == 1
    hNewFigure = figure;
    hNewAxes = axes('Parent',hNewFigure,'Box','on');
else 
    hPopupmenuStr = get(hPopupmenu,'String');
    hNewFigure = sscanf(hPopupmenuStr{get(hPopupmenu,'Value')},'%d');
    figure(hNewFigure);
    hOldAxes = findall(hNewFigure,'Type','Axes');
    if isempty(hOldAxes)
        hNewAxes = axes('Parent',hNewFigure,'Box','on');
    else
        hNewAxes = hOldAxes(end);
        set(hNewAxes,'Box','on');
    end
end
hNewLine = [];
holdState = ishold(hNewFigure);
for iLine = length(hLine):-1:1
    xdata = get(hLine(iLine),'XData');
    ydata = get(hLine(iLine),'YData');
    hold on;
    hNewLine(iLine) = plot(hNewAxes,xdata,ydata);
    hold off;
    if holdState == 1
        hold on;
    elseif holdState == 0
        hold off;
    end
    get(hLine(iLine),'Tag');
    set(hNewLine(iLine),'Tag',get(hLine(iLine),'Tag'),'DisplayName',get(hLine(iLine),'Tag'));
end
% --- set line properties
set(hNewLine,...
    'Color','b',...
    'LineStyle','-',...
    'Marker','o',...
    'MarkerSize',3,...
    'MarkerFaceColor','m'...
    );
set(hNewAxes,...
    'XGrid',get(hAxes,'XGrid'),...
    'YGrid',get(hAxes,'YGrid'),...
    'XScale',get(hAxes,'XScale'),...
    'YScale',get(hAxes,'YScale')...
    );
delete(hFigPrint2figure);   % quit print2figure

Contact us at files@mathworks.com