from Make MATLAB Talk by Sam Roberts
Make MATLAB Talk

Make MATLAB talk, second better attempt

Make MATLAB talk, second better attempt

I submitted a sharedemo previously that made MATLAB talk by using Excel.

type say_old
function say_old(myString)

xl = actxserver('excel.application');
xl.Workbooks.Add(1);
cell = xl.ActiveSheet.Range('A1');
set(cell,'Value',myString);
cell.Speak;     % Amazing!
set(xl.ActiveWorkbook,'Saved',1)
xl.Quit;

This was a hack, and Elwin Chan pointed out to me that this could be done better through a Windows API call.

type say
function say(myString,voiceName)

a = actxserver('SAPI.SpVoice.1');

if nargin > 1
    % Valid voice names are 'LH Michael', 'LH Michelle'.
    % You may be able to get more somehow.
    voicestr = ['Name=',voiceName];
else
    voicestr = 'Name=LH Michael';
end

a.Voice = a.GetVoices(voicestr).Item(0);
a.Speak(myString);

a.delete

This API call has the advantage of multiple voices.

say('I love MATLAB')

myResult = 2+3;

say(['The answer is ',num2str(myResult)],'LH Michelle');

say('Your distributed job is complete, master')

say(['The time is now',datestr(now,'HH:MM')],'LH Michelle')

say('Shut up')

say('No, you shut up','LH Michelle')

say('Both of you shut up','Microsoft Sam')

Contact us at files@mathworks.com