function googleTickers = convertYahooToGoogleTickers( yahooTickers )
% Converts ticker symbols formatted for Yahoo! Finance to those used by
% Google Finance

% Allow for a single character string as an input.  Set flag so that the
% output is a single char string, too.
if ischar(yahooTickers)
    isInputChar = true;
    yahooTickers = {yahooTickers};
else
    isInputChar = false;
end

googleTickers = cellfun(@convertYToG, yahooTickers, 'UniformOutput', false);

if isInputChar
    googleTickers = googleTickers{1};
end

end

function gTicker = convertYToG(yTicker)

sepLoc = strfind(yTicker, '.');

if ~isempty(sepLoc)
    ticker = yTicker(1:sepLoc-1);
    yExchange = yTicker(sepLoc+1:end);
    
    % The full lists are at:
    % http://www.google.com/googlefinance/disclaimer/
    % http://finance.yahoo.com/exchanges
    
    % Format: {Yahoo, Google; ...}
    lookup = {...
        'PA',   'EPA';...
        'MI',   'BIT';...
        'AS',   'AMS';...
        'DE',   'ETR';...
        'BR',   'EBR';...
        'L',    'LON';...
        'none', 'NYSE';...
        'none', 'NASDAQ';...
        'OB',   'OTC';...
        'PK',   'PINK';...
        'TO',   'TSE';...
        'V',    'CVE';...
        'ST',   'STO';...
        'CO',   'CPH';...
        'BO',   'BOM';...
        'NS',   'NSE';...
        'SS',   'SHA';...
        'SZ',   'SHE';...
        'TW',   'TPE';...
        'HK',   'HKG';...
        'SI',   'SGX';...
        'KS',   'KRX';...
        'KQ',   'KOSDAQ';...
        'TA',   'TLV';...
        'AX',   'ASX';...
        'NZ',   'NZE';...
        'none', 'MUTF'};
    
    gExchange = lookup{strcmp(yExchange, lookup(:,1)), 2};
    
    if isempty(gExchange)
        % We didn't find a match.  Likely an exchange that only Yahoo!
        % supports-- return a blank Google ticker rather than erroring out.
        gTicker = '';
    else
        gTicker = [gExchange ':' ticker];
    end
    
else
    % No separator: a US-based stock for which the Google ID is hopefully
    % the same as the Yahoo! one.  This does not always work, at which
    % point you'll have to manually add 'NASDAQ:', 'NYSE:', or the like as
    % appropriate.
    gTicker = yTicker;
    
end
end