Code covered by the BSD License  

Highlights from
Age_Calculator

image thumbnail

Age_Calculator

by

 

04 Oct 2013 (Updated )

It Just Calculates Your Age.

[]=Age_Calculator
function []=Age_Calculator

%% Initialize
base_color = (rand(1,3)+rand(1,3))./2;
set(0,'units','pixels');
position = get(0,'screensize');
height = 125;
width = 300;
cdata = generate_cdata(height,width,base_color);

%% Creat Figure
fig = figure('Name','Age_Calculator',...
    'Units','Pixels',...
    'Position',[(position(3)/2)-(width/2) (position(4)/2)-(height/2) width height],...
    'Resize','off',...
    'Menubar','none',...
    'Numbertitle','off',...
    'CloseRequestFcn',@close_age);

%% Background Axes
Baxes = axes('Parent',fig,...
    'Units','pixels',...
    'Position',[0 0 width+2 height+2],...
    'Xcolor','w',...
    'Ycolor','w',...
    'FontSize',12,...
    'Fontname','Cambria',...
    'Xtick',[],...
    'Ytick',[]);
imshow(cdata)
%% Text
text1 = text(80,20,'Enter Date of Birth',...
    'FontSize',12,...
    'Fontname','Cambria',...
    'Color','w');
%% popup day
for i = 1:31
    Array_date{i} = num2str(i);
end
popd = uicontrol('Parent',fig,...
    'Style','Popupmenu',...
    'Units','pixels',...
    'Position',[30 50 70 30],...
    'String',Array_date,...
    'FontSize',12,...
    'Fontname','Cambria',...
    'Foregroundcolor','w',...
    'Backgroundcolor',base_color.*50/height,...
    'visible','off');
%% Popup Month
Array_month = {'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'};
popm = uicontrol('Parent',fig,...
    'Style','Popupmenu',...
    'Units','pixels',...
    'Position',[120 50 70 30],...
    'String',Array_month,...
    'FontSize',12,...
    'Fontname','Cambria',...
    'Foregroundcolor','w',...
    'Backgroundcolor',base_color.*50/height,...
    'visible','off');
%% Popup Year
for i = 1901:2013
    Array_year{i-1900} = num2str(i);
end
popy = uicontrol('Parent',fig,...
    'Style','Popupmenu',...
    'Units','pixels',...
    'Position',[210 50 70 30],...
    'String',Array_year,...
    'FontSize',12,...
    'Fontname','Cambria',...
    'Foregroundcolor','w',...
    'Backgroundcolor',base_color.*50/height,...
    'visible','off');
%% Submit Button
F = getframe(Baxes,[105 10 80 30]);
push1 = uicontrol('Style','pushbutton',...
    'Units','pixel',...
    'Position',[105 10 80 30],...
    'Foregroundcolor','w',...
    'FontSize',13,...
    'Fontname','Cambria',...
    'Fontweight','bold',...
    'String','Submit',...
    'Cdata',F.cdata,...
    'Callback',@push1_callback,...
    'visible','off');


FlyIn(fig,popd,'Left','Fast')
FlyIn(fig,popm,'Bottom','Fast')
FlyIn(fig,popy,'Right','Fast')
FlyIn(fig,push1,'Top','Fast')

    function [] = push1_callback(varargin)
        D = get(popd,'Value');
        M = get(popm,'Value');
        Y = get(popy,'Value');
        Age = CalculateYourAge([D M 1900+Y]);
        set(push1,'visible','off');
        set(text1,'visible','off');
        set(popd,'visible','off');
        set(popm,'visible','off');
        set(popy,'visible','off');
        messege = ['Your Age Is ' num2str(Age(1)) ' Year(s) ' num2str(Age(2)) ' Month(s) ' num2str(Age(3)) ' day(s) '];
        text2 = text(10,60,messege,...
            'FontSize',11,...
            'Fontname','Cambria',...
            'Color','w');
        push2 = uicontrol('Style','pushbutton',...
            'Units','pixel',...
            'Position',[105 10 80 30],...
            'Foregroundcolor','w',...
            'FontSize',13,...
            'Fontname','Cambria',...
            'Fontweight','bold',...
            'String','Back',...
            'Cdata',F.cdata,...
            'Callback',@push2_callback);
        FlyIn(fig,push2,'Left','Fast')
        function [] = push2_callback(varargin)
            delete(text2);
            delete(push2);
            set(text1,'visible','on');
            FlyIn(fig,popd,'Left','Fast')
            FlyIn(fig,popm,'Bottom','Fast')
            FlyIn(fig,popy,'Right','Fast')
            FlyIn(fig,push1,'Top','Fast')
        end
    end

%% Get Cdata For Background
    function cdata = generate_cdata(height,width,base_color)
        cdata = zeros(height,width,3);
        [height width page] = size(cdata);
        for i = 1:height
            color = base_color.*(i/height);
            for j = 1:page
                cdata(i,:,j) = color(j);
            end
        end
    end
%% Close
    function close_age(varargin)
        Position = get(fig,'Position');
        for ii=1:30
            Position = [Position(1)-ii Position(2:4)];
            set(fig,'Position',Position);
            pause(0.01);
        end
        for ii=1:100
            Position = [Position(1)+ii Position(2:4)];
            set(fig,'Position',Position);
            pause(0.01);
        end
        delete(fig)
    end
%% Calculate Age
    function Age = CalculateYourAge(DateOfBirth)
        Time = clock;
        DateToday = round([Time(3) Time(2) Time(1)]);
        YY = DateToday(3) - DateOfBirth(3);
        MM = DateToday(2) - DateOfBirth(2);
        DD = DateToday(1) - DateOfBirth(1);
        if DD < 0
            DD = DD+30;
            MM = MM-1;
        end
        if MM < 0
            MM = MM+12;
            YY =YY-1;
        end
        Age = [YY MM DD];
    end

    function [] = FlyIn(FigureHandle,ObjectHandle,Direction,Speed)
        % FlyIn(FigureHandle,ObjectHandle,Direction,Speed), add Animation to the
        % ObjectHandle (i.e. axes,uicontrol) on a FigureHandle for its introduction
        % to make GUI interctive. Direction Can be 'Top','Bottom',Left' or 'Right'.
        % With Speed 'Fast' and 'Slow'.
        PositionGUI = get(FigureHandle,'Position');
        WidthGUI = PositionGUI(3);
        HeightGUI = PositionGUI(4);
        set(ObjectHandle,'Visible','off'); % Do not show Initially
        set(ObjectHandle,'Parent',FigureHandle);
        PositionObject = get(ObjectHandle,'Position'); % Final Position of Object
        X = PositionObject(1);
        Y = PositionObject(2);
        WidthObject = PositionObject(3);
        HeightObject = PositionObject(4);
        switch Speed
            case 'Fast'
                trun = 0.01;
            case 'Slow'
                trun = 0.1;
        end
        switch Direction
            case 'Top'
                set(ObjectHandle,'Visible','on'); % show
                TravelDistance = HeightGUI-Y;
                set(ObjectHandle,'Position',[X HeightGUI WidthObject HeightObject]);
                for i = 1:TravelDistance
                    set(ObjectHandle,'Position',[X HeightGUI-i WidthObject HeightObject]);
                    pause(trun);
                end
            case 'Bottom'
                set(ObjectHandle,'Visible','on'); % show
                TravelDistance = Y;
                set(ObjectHandle,'Position',[X 0 WidthObject HeightObject]);
                for i = 1:TravelDistance
                    set(ObjectHandle,'Position',[X i WidthObject HeightObject]);
                    pause(trun);
                end
            case 'Right'
                set(ObjectHandle,'Visible','on'); % show
                TravelDistance = WidthGUI-X;     
                set(ObjectHandle,'Position',[WidthGUI Y WidthObject HeightObject]);
                for i = 1:TravelDistance
                    set(ObjectHandle,'Position',[WidthGUI-i Y WidthObject HeightObject]);
                    pause(trun);
                end
            case 'Left'
                set(ObjectHandle,'Visible','on'); % show
                TravelDistance = X;
                set(ObjectHandle,'Position',[0 Y WidthObject HeightObject]);
                for i = 1:TravelDistance
                    set(ObjectHandle,'Position',[i Y WidthObject HeightObject]);
                    pause(trun);
                end
        end
end
end

Contact us