View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
text2speech - tts

4.2 | 9 ratings Rate this file 42 Downloads (last 30 days) File Size: 86.2 KB File ID: #11297 Version: 1.2

text2speech - tts


Wolfgang Garn (view profile)


04 Jun 2006 (Updated )

Converts text to speech.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information

Any text is spoken.
Get started ...
1. add the text2speech folder to your Matlab path
2. Test your new function:
   tts('This is a test.')

Casual chat.
tts('Hi - how are you?');
tts({'Hello. How are you?','It is nice to speak to you.','regards SAPI.'})

Emphasising (only for the SAPI version*)
text2speech('You can <EMPH> emphasis </EMPH> text.');

Silence (only for the SAPI version)
text2speech('There will be silence now <SILENCE MSEC=''500''/> and speech again.');

text2speech('You can <pitch middle=''-10''/> drop the pitch.');
text2speech('But you can make it <pitch middle=''+10''/> jump as well.');

*SAPI: Speech SDK 5.1 from Microsoft

Microsoft's TTS Namespace

Microsoft's Synthesizer Class


Generate Help Files From M Files, File2str, and Str2file inspired this file.

This file inspired Generate Help Files From M Files, File2str, Str2file, Number To Myriad, and Number To Words.

MATLAB release MATLAB 7.10 (R2010a)
Other requirements .NET or Microsoft SAPI (Speech SDK 5.1, Visual C++ 2005 or 2008)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (22)
13 Sep 2016 veni m

veni m (view profile)

please send the matlab code for text to speech through

Comment only

Can we add .txt file to this function ? please let me know how to voice out an .txt file in MATLAB. Thank you for the file.

05 Feb 2015 Abdullah

i wanna take the text from the output of OCR and use the tts function to make it audible
help me plz

Comment only
10 Dec 2014 Fernando Esteves

07 Nov 2014 Pushkaraj Jadhav

can we add .txt file in this function so as to read whole file

Comment only
08 Apr 2014 Nirav

Nirav (view profile)


I followed all the instructions but still get following error:

>> text2speech('Hello');
Index exceeds matrix dimensions.

Error in loadlibrary>getLoadlibraryCompilerConfiguration (line 497)
compilerConfiguration=compilerConfiguration(1); %unix machines return c and cpp compilers here

Error in loadlibrary (line 253)

Error in text2speech (line 59)


Can you please suggest what might be going wrong?


Comment only
29 Jul 2013 Aswin

Aswin (view profile)

22 Nov 2012 nausheen

Warning: If this is not a Windows system or the .Net class exists you will not be able to use this function.Please let me know what went wrong:
> In tts at 50
i got this error

11 Oct 2011 Debejyo Chakraborty

08 Feb 2011 grega

grega (view profile)

02 Feb 2011 Wolfgang Garn

Wolfgang Garn (view profile)

If you just use the "tts" function, then you should not have to download SAPI - in particular if you use Windows 7 or Vista.

Comment only
31 Jan 2011 srinu

srinu (view profile)

How can i store the speech output in .wav format.? Please help.

Comment only
30 Jan 2011 susrutha gongalla

but we need to download the microsoft SAPI first rite..???

Comment only
25 Jan 2011 Wolfgang Garn

Wolfgang Garn (view profile)

I have observed that issue as well and provided a new function based on .NET - use "tts('Hello World')" instead of text2speech. The .NET approach is significantly easier than the previous one. Please let me know whether the "emphasise", "silence", etc. features would be useful in the new version.

Comment only
30 Jun 2010 Charles

I'm running:
Matlab 7.10.0 (R2010a)
Win 7, 64bit
I get the following message upon running
??? Error using ==> loadlibrary at 480
There was an error loading the library "C:\Users\charlie\Documents\MATLAB\Sounds\text2speech\wgText2Speech.dll"
C:\Users\charlie\Documents\MATLAB\Sounds\text2speech\wgText2Speech.dll is not a valid Win32 application.

Error in ==> text2speech at 59

Caused by:
Error using ==> loaddefinedlibrary
C:\Users\charlie\Documents\MATLAB\Sounds\text2speech\wgText2Speech.dll is not a valid Win32 application.

Question: Could you provide the source code to be re-compiled in the 64bit OS?

Comment only
11 May 2010 Blake

Blake (view profile)

Sorry... I missed a single quote mark.. should read

text2speech (['count backward from' int2str(k) 'by sevens']);

11 Mar 2010 Jonathan Cofino

My computer also hangs when I attempt to use the 'text2speech' function. It worked great in MS XP, but not in MS Vista. very odd.

Comment only
10 Sep 2009 M

M (view profile)

I get stuck when loading the speech library.

This program calls MATLAB's loadlibrary function, which in turn calls the function loaddefinedlibrary. At that point, MATLAB hangs. I let it go for a few minutes and then have to force-quit MATLAB from the Task Manager. I don't get an error message, so I am not sure I have the same error as people who do get an error message about not having a module installed.

Any idea how to fix this issue? I guess it is more general than text2speech but am wondering if anyone ran across this error while using the program.

Comment only
24 Jul 2009 Wolfgang Garn

Wolfgang Garn (view profile)

Speed tag: <rate absspeed="5">, relative speed <rate speed="2"> Values are between -10 and +10.
See XML TTS tutorial ( ) for more information.

Comment only
21 Jun 2009 marian nasry

thanks for this code very much , but i've one question : how can I control the speed of the speech;
please help me

03 Nov 2007 bharath kumar

hi when i try to execute this code it says Undefined function or variable loadlibrary
can anyone plz help me

Comment only
17 Apr 2007 Nathan Miller

Works beautifully!

24 Jul 2009 1.1

Matlab 2009 release (version 7.8+) usage of SpeechSynthesizer recommended.

25 Jan 2011 1.2

Added function "tts" which uses .NET (but has no special voice features yet as in the previous text2speech version), but overcomes "Visual C++ 2005 or 2008 requirement".

Contact us