File Exchange

image thumbnail


version (465 KB) by Alfredo Fernandez
mp3 audio encoding/decoding


Updated 16 Feb 2006

View License

Editor's Note: This file was a File Exchange Pick of the Week

Includes 2 functions to write and read MP3 files. It works like the commands WAVWRITE and WAVREAD.

1.- Just unpack in the toolbox folder under the MATLAB directory.
2.- Set the MATLAB search path to include that folder.

This version was made in MATLAB for WINDOWS only.

Cite As

Alfredo Fernandez (2021). MP3WRITE and MP3READ (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (73)

Philippe DI BIN

I have a problem The following dos command
cmd=C:\Program Files\MATLAB\R2015b\toolbox\mp3_toolbox_v2.0\mp3_toolbox\mp3write\lame --quiet -b 128 C:\Program Files\MATLAB\R2015b\toolbox\mp3_toolbox_v2.0\mp3_toolbox\mp3write\temp.wav C:\Users\dibin\Desktop\test_mp3.mp3

is not functionnal in my case.
An dos error appear :

'C:\Program' is not recognized as an internal or external command,
operable program or batch file.
The "space" between Program and files in the path seems to bring trouble...

Can you help me?
Must I copy the lame function in another folder not under "program files"?

Thank you


Dheeraj M Pai

Is there a version for linux?
Ubuntu 14.04

Deniz Nakiboglu

Muhammad Naveed

Error using mp3write (line 74)
'which' is not recognized as an internal or external command,
operable program or batch file.
kindly help me about this error


I've run it successfully, thank u.

But I wonder what's the difference between mp3read (better accuracy?)and wavread?

Zijie Yuan

Adil khan


For those who are confused about the Line 67/ line 50 errors, I think I've found the answer. Presumably you all are not using Windows - which becomes an issue when the mp3read function attempts to access mpg123.exe (as well as lame.exe and others). Exe files are not executable by macs or linux... hence the problem. The reason wavread can't open the file is because there's nothing in it. It's still the temporary wav file that tempfile was initialized with.

Fortunately, there are mpg123 and lame files that have been made for mac and linux. You can find them here: Put the .mac or linux files in your directory and you should be fine.

Then just substitute this code in for line 42 (if you're a mac user):
[stat,raw_info] = dos(['"' location,'/mpg123.mac"', ' -w ', tmpfile, ' ', '"',FILE,'"']);

Hope this works for ya'll.

Aravind Vasudevan


I am trying to read an mp3 file using the mp3read function. And I am always getting this error :

Error using wavread (line 67)
Invalid Wave File. Reason: Cannot open file.

Error in mp3read (line 50)
[Y,FS,NBITS] = wavread(tmpfile); % Load the
data and delete temporary file

Anybody knows any solution for this? Any help would be much appreciated.



out of memory problem. any suggestions?



To anyone getting the error "??? Error using ==> wavread at 67
Cannot open file.", I was getting the same, but once I moved all of the mpg123, mp3info, and lame files to the same folder as the program that was running them, it worked perfectly.

Mr Smart


change line 42 in mp3read to

[stat,raw_info] = dos(['"' location,'\mpg123.exe"', ' -w ', tmpfile, ' ', '"',FILE,'"']);


Paulo Emmanuel

same error here

??? Error using ==> wavread at 166
Out of memory. Type HELP MEMORY for your options.

Error in ==> mp3read at 48
[Y,FS,NBITS] = wavread(tmpfile); % Load the data and delete temporary file

Error in ==> AudioComp at 4
[y, Fs] = mp3read(gfile);


This code is very good,but i can't run it. Everytime i try to run, Matlab always notifies:
??? Error using ==> wavread at 67
Cannot open file.

??? Error in ==> mp3read at 49
[Y,FS,NBITS] = wavread(tmpfile); % Load the data and delete
temporary file
So please show me what's the problem?

Arjen vd Kamp

If you have the error in line 48 of the mp3read then there probably is a space in the directory were you put the mp3read.m. For instance between Program and Files. Just put it in a folder without spaces and try again...

Arjen vd Kamp

chen xy

I got the same error!
anyone can help me?
??? Error using ==> wavread at 67
Cannot open file.

Error in ==> mp3read at 48
[Y,FS,NBITS] = wavread(tmpfile); % Load the data and delete
temporary file

Jun wan


why i got this message?
i hope u can help me. tq in advance. ;]

??? Error using ==> wavread at 67
Cannot open file.

Error in ==> mp3read at 48
[Y,FS,NBITS] = wavread(tmpfile); % Load the data and delete temporary file

Deyan Dobromirov

Its awesome THANK U!

Deyan Dobromirov

It worked 4 me its awesome guys :D Congrats to all!!

John Adcox

Please fix the pathname issue that John Hughes mentioned earlier. His fix works so you will just need to upload a new file


Can the MP3READ function be used to detect the frequency, tempo and rhythm of an mp3 file? Where are these values stored for retreval and comparison in the future?

sura mohamed

i want another idea on voice project
i want to make a reaserch in sound

naimat khan

i am the student of electrical engineering at uet peshawar

palani natarajan


hung duc

I'm working in watermarking, but I don't understand clearly. So, I need some examples or Matlab code to simulate. Please help me

Luigi Rosa

Very good. In mp3read.m the output variable "out" is unnecessary, probably introduced for debugging.

Michael McGurie

A MEX version of this program would give it a perfect score. Very useful for playing around with sound files.

Punit master

remove your site & code from google
your code is pathetic

John Hughes

Fix for lines 54-70 of mp3write.m:

This allows one to use pathnames with spaces in them.

Note that the who thing fails if you give an ABSOLUTE pathname (like "C:/Matlab-stuff/mySong.mp3"), because the read/write routines expect only relative pathnames. I haven't tried to fix this part.

tmpfile = ['"', strcat(lame,'\temp.wav'), '"'];
MP3FILE = ['"', strcat(pwd,'\',MP3FILE), '"'];
case {'1'}
cmd = ['"', lame,'\lame', '"', ' --quiet', ' ', tmpfile, ' ',MP3FILE];
case {'2'}
cmd = ['"',lame,'\lame', '"', ' --quiet', ' -b 128 ', tmpfile, ' ',MP3FILE];
case {'3'}
cmd = ['"',lame,'\lame', '"', ' --quiet', ' --abr 112 ', tmpfile, ' ',MP3FILE];
case {'4'}
cmd = ['"',lame,'\lame', '"', ' --quiet', ' -f ', tmpfile, ' ',MP3FILE];
case {'5'}
cmd = ['"',lame,'\lame', '"',' --quiet', ' -h ', ' -V ', tmpfile, ' ',MP3FILE];
error('Encoding parameters not suported')

John Hughes

Suggested code mod for the reader:
Line 42:
[stat,raw_info] = dos(['"',location,'\mpg123','"', ' -w ', tmpfile, ' ', '"',FILE,'"'])

By putting double-quotes around the pathname to mpg123, I've fixed the "can't handle blanks in the path" problem. Presumably a similar fix is needed for mp3write; I'll send it when/if I get there...

Mara M

It seems the search path needs to be w/o blanks for it to work. Also there was one row in mp3write changing the path to a name which didn't work out (row 55: MP3FILE = strcat(pwd,'\',MP3FILE)).
Otherwise, great job.

per g

2 columns = stero sound? Haven't tried the toolbox, just guessing!+

famous martin

very very good

Jose Uy

Why does it output 2 columns? Is one column the time index and the other is the amplitude?

Andy Clark

Great bit of code! Now I can analyse mp3's in matlab.

huang baozhu

when i use mp3write ,it say mp3buffer is not too big enough

mauricio peña

shu tong

Juan Carlos Ruchesi

very good

guan liang

very good

René Gonález

It's a very good rutine

Yohn Eduin Parra

Good aplication. I need more time to evaluation and qualify to.

laeeqsdq neelofer

sathish Reddy

Somendra Misra

Joao Valiati

albert crespo

Rubén Sánchez

Jurijs Jonass

Thanks Alfredo for nice job!

Nanno Mulder

I cannot open the .zip file, so low marks for the packaging. No comments on the contents

tran huy dong

Victor Colme

nO Entendì una mierda!!!

jaydeep dhole

very helpful

Matevz Leskovsek

Nice work. Very well commented!

vinay dev


saifudeen ar

Johnson Yeh

Junjie Liu

Ely Tejeda

jeeva rajan

3/2 ramachandra st
off north usman road
t nagar

jeeva rajan

for modified code contact me
3/2 ramachandra st
off north usman road
t.nagar chennai-17

marcin marcin

Murphy O'Brien

Does exactly what it says on the tin! A very useful complement to wavread and wavwrite.

Sandeep Sanga


Dave Robinson

Works for me - ACE

Mahmoud Bagheri

for me , its fantastic.

irfan turk

joakim brännström

MATLAB Release Compatibility
Created with R14
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!