function result = convertMorseAthruF(inputString) %convertMorseAthruF Convert letters A through F to Morse Code % % consumes: inputString, a string of characters (only spaces and A-F) % produces: result, a string in morse code % % notes: % The output string will have . for dits, % --- for a dash (with a duration of three dits) % and spaces for a silent period equal to one dit. % % Examples: % >> convertMorseAthruF('error'); % Error: all chars in input string must be A-F % >> convertMorseAthruF('FEED'); % ans = % '. . --- . . . --- . .' % >> convertMorseAthruF('FEED ABBA BEEF'); % ans = % ['. . --- . . . --- . . ' ... % '. --- --- . . . --- . . . . --- ' ... % '--- . . . . . . . --- .'] % >> % % the vector "letters" is a vector of the legal characters. % the space character is treated as the inter-word-space. letters = 'ABCDEF '; % set up table of letters A through F, and inter-word-space morse{1} = '. ---'; % A morse{2} = '--- . . .'; % B morse{3} = '--- . --- .'; % C morse{4} = '--- . .'; % D morse{5} = '.'; % E morse{6} = '. . --- .'; % F morse{7} = ' '; % inter-word-space % loop through inputString. % for each character, find its position in the letters string. % add the corresponding element of the cell array morse to the % result. % if the character is NOT in the letters string, throw an error result = ''; for k=1:length(inputString) position = strfind(letters,inputString(k)); if (length(position) == 0) error(['Character "' inputString(k) '" in position ' num2str(k) ... ' is not permitted']); else % if this isn't the first character, and the previous % character wasn't a space, and this character isn't a space % then add the space-between-chars if (k>1 && inputString(k-1)~=' ' && inputString(k)~=' ') result = [ result ' ' ]; end % if % add this character into the string result = [ result morse{position} ]; end % if end % for return; end