No BSD License  

Highlights from
GUI - Calender Convertor

image thumbnail
from GUI - Calender Convertor by Saim
Converts the calender format from gregorian to julian and vice-versa

calender_convert(varargin)
function varargout = calender_convert(varargin)
% CALENDER_CONVERT M-file for calender_convert.fig
%      CALENDER_CONVERT, by itself, creates a new CALENDER_CONVERT or raises the existing
%      singleton*.
%
%      H = CALENDER_CONVERT returns the handle to a new CALENDER_CONVERT or the handle to
%      the existing singleton*.
%
%      CALENDER_CONVERT('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in CALENDER_CONVERT.M with the given input arguments.
%
%      CALENDER_CONVERT('Property','Value',...) creates a new CALENDER_CONVERT or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before calender_convert_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to calender_convert_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help calender_convert

% Last Modified by GUIDE v2.5 05-Feb-2008 13:04:14

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @calender_convert_OpeningFcn, ...
                   'gui_OutputFcn',  @calender_convert_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before calender_convert is made visible.
function calender_convert_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to calender_convert (see VARARGIN)

% Choose default command line output for calender_convert
handles.output = hObject;
set(handles.gregjul,'value',1);
set(handles.julgreg,'value',0);
set(handles.text1,'visible','on');
set(handles.text2,'visible','on');
set(handles.text3,'visible','on');
set(handles.text4,'visible','on');
set(handles.text5,'visible','on');
set(handles.text6,'visible','on');
set(handles.year,'visible','on');
set(handles.month,'visible','on');
set(handles.day,'visible','on');
set(handles.hour,'visible','on');
set(handles.minute,'visible','on');
set(handles.second,'visible','on');
set(handles.julian,'visible','on');
set(handles.greg,'visible','off');
set(handles.disptext,'string','Julian: ')
set(handles.juliandate,'string','Date: ');
set(handles.julianday,'string','Day: ')
set(handles.Juldate,'visible','off');
set(handles.juldate1,'visible','off');
set(handles.time,'visible','off');
set(handles.timedisp,'visible','off');

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes calender_convert wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = calender_convert_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;



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

% Hints: get(hObject,'String') returns contents of year as text
%        str2double(get(hObject,'String')) returns contents of year as a double


% --- Executes during object creation, after setting all properties.
function year_CreateFcn(hObject, eventdata, handles)
% hObject    handle to year (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



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

% Hints: get(hObject,'String') returns contents of month as text
%        str2double(get(hObject,'String')) returns contents of month as a double


% --- Executes during object creation, after setting all properties.
function month_CreateFcn(hObject, eventdata, handles)
% hObject    handle to month (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



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

% Hints: get(hObject,'String') returns contents of day as text
%        str2double(get(hObject,'String')) returns contents of day as a double


% --- Executes during object creation, after setting all properties.
function day_CreateFcn(hObject, eventdata, handles)
% hObject    handle to day (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



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

% Hints: get(hObject,'String') returns contents of hour as text
%        str2double(get(hObject,'String')) returns contents of hour as a double


% --- Executes during object creation, after setting all properties.
function hour_CreateFcn(hObject, eventdata, handles)
% hObject    handle to hour (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



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

% Hints: get(hObject,'String') returns contents of minute as text
%        str2double(get(hObject,'String')) returns contents of minute as a double


% --- Executes during object creation, after setting all properties.
function minute_CreateFcn(hObject, eventdata, handles)
% hObject    handle to minute (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



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

% Hints: get(hObject,'String') returns contents of second as text
%        str2double(get(hObject,'String')) returns contents of second as a double


% --- Executes during object creation, after setting all properties.
function second_CreateFcn(hObject, eventdata, handles)
% hObject    handle to second (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in greg.
function greg_Callback(hObject, eventdata, handles)
% hObject    handle to greg (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
JD = str2num(get(handles.Juldate,'string'));
[year,month,day,hour,minu,sec,dayweek,dategreg] = julian2greg(JD) ;
x = sprintf('%d/%d/%d',day,month,year);
set(handles.juldatdisp,'string',x);

y = sprintf('%d:%d:%d',hour,minu,sec);
set(handles.timedisp,'string',y);

z = sprintf('%s',dayweek);
set(handles.juldaydisp,'string',z);

% --- Executes on button press in julian.
function julian_Callback(hObject, eventdata, handles)
% hObject    handle to julian (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
year  = str2double(get(handles.year, 'string'));
month = str2double(get(handles.month,'string'));
day  = str2num(get(handles.day,   'string'));
hour = str2num(get(handles.hour,  'string'));
min  = str2num(get(handles.minute,'string'));
sec  = str2num(get(handles.second,'string'));


[a b]= greg2julian(year,month,day,hour,min,sec);
set(handles.juldatdisp,'string',num2str(a));
set(handles.juldaydisp,'string',num2str(b));


function [JD,julianday] =greg2julian(year,month,day,hour,min,sec)
% -----------------------------------------------------------------------------------------------------------
% This function converts the Gregorian dates to Julian dates.
%
% 0. Syntax:
% [JD,julianday] = juliandate(year,month,day,hour,min,sec)
%
% 1. Inputs:
%     year, month, day = date in Gregorian calendar.
%     hour,min,sec = time at universal time.
%
% 2. Outputs:
%     JD = Julian date.
%     julianday = day of week.
%
% 3. Example:
%  >> [a,b] = greg2julian(2006,5,30,2,30,28)
%  a = 
%
%           2453885.60449074
%  b =
%
%  Tuesday
% 
%  4. Notes:
%     - For all common era (CE) dates in the Gregorian calendar, and for more
%     information, check the referents.
%     - The function was tested, using  the julian date converter of U.S. Naval Observatory and
%     the results were similar. You can check it.
%     - Trying to do the life... more easy with the conversions.
%
% 5. Referents:
%     Astronomical Applications Department. "Julian Date Converter". From U.S. Naval Observatory.
%               http://aa.usno.navy.mil/data/docs/JulianDate.html
%     Duffett-Smith, P. (1992).  Practical Astronomy with Your Calculator.
%               Cambridge University Press, England:  pp. 9.
%     Seidelmann, P. K. (1992). Explanatory Supplement to the Astronomical Almanac. 
%               University Science Books, USA.  pp. 55-56.
%      Weisstein, Eric W.  "Julian Date".  From World of Astronomy--A Wolfram Web Resource. 
%               http://scienceworld.wolfram.com/astronomy/JulianDate.html
%
% Gabriel Ruiz Mtz.
% May-2006
%
% Modifications:
% 04/06/06: To find the days, it was only changed the loop to a cell array. Thanks to Jrme.
% ------------------------------------------------------------------------------------------------------------
   
   error(nargchk(6,6,nargin))
   timeut = hour + ( min / 60 ) + ( sec / 3600 );

   %For common era (CE), anno domini (AD)
   JD = ( 367 * year ) - floor ( 7 * ( year + floor( ( month + 9 ) / 12 ) ) / 4 ) - ...
                     floor( 3 * ( floor( ( year + ( month - 9 ) / 7 ) / 100 ) + 1 ) / 4 ) + ...
                     floor( ( 275 * month ) / 9 ) + day + 1721028.5 + ( timeut / 24 );
   a = ( JD + 1.5 ) / 7;
   frac = a - floor(a);
   n = floor(frac * 7) ;
   julianday ={ 'Sunday' 'Monday' 'Tuesday' 'Wednesday' 'Thursday' 'Friday' 'Saturday'};
   julianday = julianday{n+1};







% --- Executes on button press in reset.
function reset_Callback(hObject, eventdata, handles)
% hObject    handle to reset (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.year,  'string',' ');
set(handles.month, 'string',' ');
set(handles.day,   'string',' ');
set(handles.hour,  'string',' ');
set(handles.minute,'string',' ');
set(handles.second,'string',' ');
set(handles.juldatdisp,'string',' ');
set(handles.juldaydisp,'string',' ');
set(handles.timedisp,'string',' ');
set(handles.Juldate,'string',' ');


% --- Executes on button press in gregjul.
function gregjul_Callback(hObject, eventdata, handles)
% hObject    handle to gregjul (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%clear all the content
set(handles.year,  'string',' ');
set(handles.month, 'string',' ');
set(handles.day,   'string',' ');
set(handles.hour,  'string',' ');
set(handles.minute,'string',' ');
set(handles.second,'string',' ');
set(handles.juldatdisp,'string',' ');
set(handles.juldaydisp,'string',' ');
set(handles.timedisp,'string',' ');
set(handles.Juldate,'string',' ');

% Hint: get(hObject,'Value') returns toggle state of gregjul
set(handles.gregjul,'value',1);
set(handles.julgreg,'value',0);
set(handles.text1,'visible','on');
set(handles.text2,'visible','on');
set(handles.text3,'visible','on');
set(handles.text4,'visible','on');
set(handles.text5,'visible','on');
set(handles.text6,'visible','on');
set(handles.year,'visible','on');
set(handles.month,'visible','on');
set(handles.day,'visible','on');
set(handles.hour,'visible','on');
set(handles.minute,'visible','on');
set(handles.second,'visible','on');
set(handles.julian,'visible','on');
set(handles.greg,'visible','off');
set(handles.disptext,'string','Julian: ')
set(handles.juliandate,'string','Date: ');
set(handles.julianday,'string','Day: ')
set(handles.Juldate,'visible','off');
set(handles.juldate1,'visible','off');
set(handles.time,'visible','off');
set(handles.timedisp,'visible','off');

% --- Executes on button press in julgreg.
function julgreg_Callback(hObject, eventdata, handles)
% hObject    handle to julgreg (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%clear all the content
set(handles.year,  'string',' ');
set(handles.month, 'string',' ');
set(handles.day,   'string',' ');
set(handles.hour,  'string',' ');
set(handles.minute,'string',' ');
set(handles.second,'string',' ');
set(handles.juldatdisp,'string',' ');
set(handles.juldaydisp,'string',' ');
set(handles.timedisp,'string',' ');
set(handles.Juldate,'string',' ');

% Hint: get(hObject,'Value') returns toggle state of julgreg
set(handles.gregjul,'value',0);
set(handles.julgreg,'value',1);
set(handles.text1,'visible','off');
set(handles.text2,'visible','off');
set(handles.text3,'visible','off');
set(handles.text4,'visible','off');
set(handles.text5,'visible','off');
set(handles.text6,'visible','off');
set(handles.year,'visible','off');
set(handles.month,'visible','off');
set(handles.day,'visible','off');
set(handles.hour,'visible','off');
set(handles.minute,'visible','off');
set(handles.second,'visible','off');
set(handles.julian,'visible','off');
set(handles.greg,'visible','on');
set(handles.disptext,'string','Gregorian: ')
set(handles.juliandate,'string','Date: ');
set(handles.julianday,'string','Day: ')
set(handles.Juldate,'visible','on');
set(handles.juldate1,'visible','on');
set(handles.time,'visible','on');
set(handles.timedisp,'visible','on');
set(handles.time,'string','Time:');



function [year,month,day,hour,minu,sec,dayweek,dategreg] = julian2greg(JD)
% ______________________________________________________________________________________________
% This function converts the Julian dates to Gregorian dates.
%
% 0. Syntax:
% [day,month,year,hour,min,sec,dayweek] = julian2greg(JD)
%
% 1. Inputs:
%     JD = Julian date.   
%
% 2. Outputs:
%     year, month, day = date in Gregorian calendar.
%     hour, min, sec,dayweek = time at universal time.
%
% 3. Example:
%  >> [a,b,c,d,e,f,g,h] = julian2greg(2453887.60481)
%  a = 
%     2006      
%  b =
%     6
%  c =
%     1
%  d =
%     2
%  e =
%     30
%  f =
%     56
%  g = 
%     Thursday
%  h =
%       1     6     2006     2     30     56
%
% 4. Notes:
%     - For all common era (CE) dates in the Gregorian calendar.
%     - The function was tested, using  the julian date converter of U.S. Naval Observatory and
%     the results were similar. You can check it.
%     - Trying to do the life... more easy with the conversions.
%
% 5. Referents:
%     Astronomical Applications Department. "Julian Date Converter". From U.S. Naval Observatory.
%               http://aa.usno.navy.mil/data/docs/JulianDate.html
%     Duffett-Smith, P. (1992).  Practical Astronomy with Your Calculator.
%               Cambridge University Press, England:  pp. 8,9.
%
% Gabriel Ruiz Mtz.
% Jun-2006
% ____________________________________________________________________________________________

error(nargchk(1,1,nargin))

I = floor( JD + 0.5);
Fr = abs( I - ( JD + 0.5) );	 

if I >= 2299160 
     A = floor( ( I- 1867216.25 ) / 36524.25 );
     a4 = floor( A / 4 );
     B = I + 1 + A - a4;
else
     B = I;
end 

C = B + 1524;
D = floor( ( C - 122.1 ) / 365.25 );
E = floor( 365.25 * D );
G = floor( ( C - E ) / 30.6001 );
day = floor( C - E + Fr - floor( 30.6001 * G ) );

if G <= 13.5 
    month = G - 1;
else
    month = G - 13;
end

if month > 2.5
    year = D - 4716;
else
    year = D - 4715;
end

hour = floor( Fr * 24 );
minu = floor( abs( hour -( Fr * 24 ) ) * 60 );
minufrac = ( abs( hour - ( Fr * 24 ) ) * 60 ); 
sec = ceil( abs( minu - minufrac ) * 60);
AA = ( JD + 1.5 ) / 7;
nd = floor( (abs( floor(AA) - AA ) ) * 7 );
dayweek ={ 'Sunday' 'Monday' 'Tuesday' 'Wednesday' 'Thursday' 'Friday' 'Saturday'};
dayweek = dayweek{ nd+1};
format('long', 'g');
dategreg = [ day month year hour minu sec ];
	   




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

% Hints: get(hObject,'String') returns contents of Juldate as text
%        str2double(get(hObject,'String')) returns contents of Juldate as a double


% --- Executes during object creation, after setting all properties.
function Juldate_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Juldate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end




% --------------------------------------------------------------------
function aboutus_Callback(hObject, eventdata, handles)
% hObject    handle to aboutus (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
 msgbox('Calender Converter GUI Subin Thomas (c) 2008 thomas.subin@gmail.com')

Contact us at files@mathworks.com