Code covered by the BSD License  

Highlights from
Yahoo Weather - GUI

image thumbnail
from Yahoo Weather - GUI by Aravind Seshadri
A GUI to get the latest weather information (Based on yweather() by A. Belani)

yahoo_weather(varargin)
function varargout = yahoo_weather(varargin)
% YAHOO_WEATHER M-file for yahoo_weather.fig
%      YAHOO_WEATHER, by itself, creates a new YAHOO_WEATHER or raises the existing
%      singleton*.
%
%      H = YAHOO_WEATHER returns the handle to a new YAHOO_WEATHER or the handle to
%      the existing singleton*.
%
%      YAHOO_WEATHER('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in YAHOO_WEATHER.M with the given input arguments.
%
%      YAHOO_WEATHER('Property','Value',...) creates a new YAHOO_WEATHER or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before yahoo_weather_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to yahoo_weather_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 yahoo_weather

% Last Modified by GUIDE v2.5 21-Mar-2006 01:07:54

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @yahoo_weather_OpeningFcn, ...
                   'gui_OutputFcn',  @yahoo_weather_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 yahoo_weather is made visible.
function yahoo_weather_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 yahoo_weather (see VARARGIN)

% Choose default command line output for yahoo_weather
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes yahoo_weather wait for user response (see UIRESUME)
% uiwait(handles.yahoo_weather_figure);

setappdata(0  , 'WeatherGui'    , gcf);

% A simple flag to check required files
err = 0;

setappdata(gcf, 'err', err);
%% Confirm presence of xml_parse function
if isempty(which('xml_parse'))
    err = 'close';
    clc;
    error = strvcat('Warning! Unable to execute the function.',...
           'The function xml_parse was not found. Ensure that the XML Toolbox by Marc Molinari,',...
           'available on the MATLAB Central File Exchange, is installed. The URL to it is',...
           'http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=4278&objectType=file');
    disp(error)
    setappdata(gcf, 'err', err);
end


% --- Outputs from this function are returned to the command line.
function varargout = yahoo_weather_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;


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


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



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

% --- Executes on button press in enter_button.
function enter_button_Callback(hObject, eventdata, handles)
% hObject    handle to enter_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

location = get(handles.zipcode,'String');
units = get(handles.english_units, 'Value');
% Just a simple switch used to get the units information. The units
% arguments are for yweather function.
switch units
    case 0
        units = 'c';
    case 1
        units = 'f';
end

WeatherGui = getappdata(0, 'WeatherGui');
err = getappdata(WeatherGui, 'err');

if err == 'close'
    error_message = strvcat('xml_parse error!',...
        'Follow command window instructions.');
    error_dialog_box(error_message);
    close(WeatherGui);
else
    [info, error] = yweather(location, units);
    a = '  ';
    switch error
        case 0
            direction = info.yweather_wind.direction;
            % Find out the direction of wind
            if direction < 22.5 & direction >= 337.5
                direction = 'N';
            elseif direction >= 22.5 && direction < 67.5
                direction = 'NE';
            elseif direction >= 67.5 && direction < 112.5
                direction = 'E';
            elseif direction >= 112.5 && direction < 157.5
                direction = 'SE';
            elseif direction >=157.5 && direction < 202.5
                direction = 'S';
            elseif direction >=202.5 && direction < 247.5
                direction = 'SW';
            elseif direction >= 247.5 && direction < 292.5
                direction = 'W';
            else
                direction = 'NW';
            end
            % Set all the information in corresponding static text elements
            set(handles.text_title, 'String', info.item.title);
            set(handles.text_current_temperature, 'String', [num2str(info.item.yweather_condition.temp) a info.yweather_units.temperature]);
            set(handles.text_current_text, 'String', info.item.yweather_condition.text);
            set(findobj('Tag', 'yahoo_weather_figure'), 'Name', info.title);
            set(handles.text_temperature_high, 'String', [num2str(info.item.yweather_forecast(1,1).high) a info.yweather_units.temperature]);
            set(handles.text_temperature_low, 'String', [num2str(info.item.yweather_forecast(1,1).low) a info.yweather_units.temperature]);
            information_data_1 = strvcat([num2str(info.yweather_wind.chill) a info.yweather_units.temperature],...
                [num2str(info.yweather_atmosphere.humidity) ' %'], ...
                [num2str(info.yweather_atmosphere.pressure) a info.yweather_units.pressure], ...
                [direction a num2str(info.yweather_wind.speed) a info.yweather_units.speed], ...
                [info.yweather_astronomy.sunrise], ...
                [info.yweather_astronomy.sunset], ...
                [num2str(info.item.geo_lat)], ...
                [num2str(info.item.geo_long)]);
            set(handles.additional_information_data, 'String', information_data_1);    
            forecast_text = info.item.yweather_forecast(1,2).text;
            spacing = strfind(forecast_text, ' ');
            if length(forecast_text) > 15
                forecast_text = strvcat(forecast_text(1:spacing),...
                    forecast_text(spacing + 1 : length(forecast_text)));
            end
            information_data_2 = strvcat(info.item.yweather_forecast(1,2).day,...
                info.item.yweather_forecast(1,2).date,...
                [num2str(info.item.yweather_forecast(1,2).high) a info.yweather_units.temperature],...
                [num2str(info.item.yweather_forecast(1,2).low) a info.yweather_units.temperature],...
                forecast_text);
            set(handles.additional_information_data_2, 'String', information_data_2);            
        case 1
            % Unable to connect to internet.
            error_message = strvcat('Unable to connect to internet!',...
                'Check your internet connection.');
            error_dialog_box(error_message);
        case 2
            % Invalid city, city not found.
            error_message = strvcat('Invalid city!',...
                'Check your zipcode.');
            error_dialog_box(error_message);
    end
end


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


function zipcode_Callback(hObject, eventdata, handles)
% hObject    handle to zipcode (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 zipcode as text
%        str2double(get(hObject,'String')) returns contents of zipcode as a double
s = str2double(get(hObject,'String'));
if isnan(s)
    set(hObject,'String','Invalid')
end

% --- Executes during object creation, after setting all properties.
function zipcode_CreateFcn(hObject, eventdata, handles)
% hObject    handle to zipcode (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


Contact us at files@mathworks.com