View License

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

» Watch video

Highlights from
Web Server

5.0 | 16 ratings Rate this file 57 Downloads (last 30 days) File Size: 104 KB File ID: #29027 Version: 1.4
image thumbnail

Web Server


Dirk-Jan Kroon (view profile)


14 Oct 2010 (Updated )

HTTP Matlab Web Server, allows m-files to be executed in a browser

| Watch this File

File Information

This function WEBSERVER, is a HTTP webserver for HTML code and images and also allows execution of Matlab code through the web.


   - HTML and images
   - Sub-Folders
   - Upload files
   - M file execution

  % Use firefox or internet explorer and visit http://localhost:4000/

MATLAB release MATLAB 7.10 (R2010a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (30)
14 Sep 2016 Armin Ghasem Azar

Hi. Thanks for this nice application. I have a problem in photo uploading. I select an image named 1.jpg. Whatever I write in box "Title of your Image", I get the following error after sending:

Error using fwrite
Invalid file identifier. Use fopen to generate a valid file identifier.

I went through the code and it seems that it doesn't save the image in any folder. Then, it creates a random number and assigns it to the image's name. But , finally, it can't find the image. (see file multipart2struct.m, lines36 to 41).

How can I solve this problem?

19 Jun 2016 avcsw pk

04 Jun 2016 Aaron Barker

This webserver is fantastic. Using it to provide some MATLAB analysis functions to co-workers.

Does this support serving css or am I missing something? I don't seem to be able to use it.

Comment only
20 Apr 2016 Edgard Salas

Hi, A favour, when I write in portuguese language some letters (for example: ç,õ,í,á, ) does not appear when the text arrives to the matlab. How can I resolve it?
I thank you since now for your help.

Comment only
19 Apr 2015 Ron

Ron (view profile)

Wish MATLAB could be used for web application like other framework e.g., web2py. This submission shows its possible to use MATLAB as a front end for the framework. Wondering if any plans to move fwd to something bigger, like ORM for django?

19 Apr 2015 Ron

Ron (view profile)

04 Mar 2015 Jan Balaguer

01 Apr 2014 Dirk-Jan Kroon

Dirk-Jan Kroon (view profile)

This software is only a small demo I wrote for fun, and never tested with more than one user.

Probably it is also not really suitable for multiple users, because Matlab is in basis single threaded.

A solution is using a real HTTP server like Apache, in combination with PHP (see
Then compile your matlab-code to a executable and simply parse the user inputs/outputs to that executable through PHP.

Comment only
01 Apr 2014 CdC

CdC (view profile)

Thank you again for creating this fantastic tool. It will have a very big impact on our progress.

Question: Is there a way to decrease response latency from the server? We have the server up and running, but we are getting latencies to serve a simple page of about 1 second at no load, and up to 10 seconds at larger numbers of users (still pretty modest numbers in the tens of concurrent users). We would like to use it with a capacity to handle about 100 concurrent users with latencies <1second. Is this possible? Will running on a faster machine, compiling, or other approaches lead to this outcome? Thank you very much for your help.

It turns out that the post below from 30 Mar 2014 saying that the webserver was not working results from the fact that the webserver doesn't work with Matlab2007b. On newer Matlab versions webserver works fine (2010, 2014).

Thanks again.

30 Mar 2014 CdC

CdC (view profile)

Thanks for contributing, as this looks like it could be an important application for us.
Unfortunately, so far the code is not working for me. Help would be greatly appreciated.

I downloaded and unzipped the files and subfolders into a directory, went to that directory in matlab, and typed the sample commands, eg webserver(4000) or webserver(4000,config) after setting up config variable. The webserver seemed to have started, and created a figure window. I then went to chrome and firefox and typed http://localhost:4000, but I got the error that the connection timed out / no data received. I tried again with port 4001, same error. This is on a windows pc. Note this is with Matlab 2007b. Thanks for your help.

Comment only
09 Mar 2014 MatlabNewbie

How can i get the Multipart POST to work for binary data that i'm sending? I'm not sending a file but a 8 bit grayscale PNG image byte array. The code is failing completely in text2header. Is this even supported?

Comment only
24 Feb 2014 Rahim

Rahim (view profile)

Where would I go about placing my own m file for execution within the code, as in which .m file would I place the call under?

Comment only
08 Feb 2014 Yves

Yves (view profile)

Very useful. Thank you.

Please note I had to split:

into 2 lines:

To avoid the error:

Static method or constructor invocations cannot be indexed.
Do not follow the call to the static method or constructor with
any additional indexing or dot references.”

11 Oct 2013 Kirill

Kirill (view profile)

14 Aug 2013 taoufik wassar

Thank you

Comment only
23 Jun 2013 Omar Shenawy

This is just perfect in every manner. Awesome piece of code that is well designed and works perfectly :-)

19 Jun 2013 Suresh Deoda

Great work, thanks a lot.

11 Oct 2012 Dirk-Jan Kroon

Dirk-Jan Kroon (view profile)

*Filipe S.
A Matlab GUI/Figure is user-interface thus client-side, this is a webserver thus server-side.
The only way to do client-side things like showing a GUI, is manually coding the GUI in javascript, html and css

Comment only
10 Oct 2012 Filipe S.

i'm trying to use this to put a matlab gui on a web page. However, i'm stubling on how to call the .m file to call the gui. Can you help me on how to do this?


Comment only
02 Aug 2012 Michele Parmigiani

Excellent work with many useful applications.

Anyway I experienced one problem and I hope you can help me to understand how to solve it.
Web server application runs very well on my laptop (Matlab7.11.1.866 (R2010b) SP1), but when I try to run in my virtual machine (Matlab7.1.0.246 (R14) SP3) the server starts, but when I try to connect the brower start to load the page but it stops and the display message is the following:

Webserver Available on http://localhost:4000/
GET / HTTP/1.1

Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/x-ms-application, application/x-ms-xbap, application/xaml+xml, */*

Accept-Language: en-gb

User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 1.1.4322; .NET4.0C)

Accept-Encoding: gzip, deflate

Host: localhost:4000

Connection: Keep-Alive

Get: [1x1 struct]
Content: [1x1 struct]

HTTP/1.0 200 OK
Date: Tue, 12 Oct 2010 09:19:05 GMT
Server: Matlab Webserver
X-Powered-By: Matlab7.1.0.246 (R14) Service Pack 3
Set-Cookie: SESSID=5322082bf473207961031e3df1f45a22; path=/
Expires: Thu, 19 Nov 1980 08:00:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Connection: close
Content-Length: 336
Content-Type: text/html; charset=UTF-8

I tried to locate the source of the problem and it seems that after the first while(true) the script enter in:
and it get stuck into:
try socket = TCP.serverSocket.accept; break; catch, end
if(~ishandle(TCP.stopwindow)), TCP.socket=-1; return, end

Any suggestion is very welcomed!

Best Regards,

Michele Parmigiani

09 Feb 2012 Changshun Deng

Does this need appach or IIS?

Comment only
18 Oct 2011 Mathias Bustamante

Excelent piece of code, very useful !

28 Sep 2011 Ernest

Ernest (view profile)

Hi.. how do I automatically reset the webserver whenever I encounter this error:

Java exception occurred: Software caused connection abort: socket write error

That is, I dont need to go to matlab and re-run webserver(80) manula in the command window.

Thank you.

Comment only
30 Aug 2011 today

today (view profile)

24 Mar 2011 Adam Leadbetter

Very nice piece of code. It works very well for me.

04 Feb 2011 Aleksandar Pavic

Very interesting, I will test this, I've made php-console bridge on linux...

Comment only
16 Dec 2010 Nicolas Billard

I can confirm it works well.

25 Nov 2010 Dirk-Jan Kroon

Dirk-Jan Kroon (view profile)

*Nicolas Billard
Today I uploaded the version which solves your problems (will probably be online tomorrow) ...

Comment only
27 Oct 2010 Dirk-Jan Kroon

Dirk-Jan Kroon (view profile)

*Nicolas Billard

Thank you for your comments.

The Java-error is due to earlier usage of the port without correctly closing/disconnecting the port due to a code-crash or other problem. In the future I will probably try to solve that by storing the port in a Matlab global, making it possible to close the port after a code crash.

I didn't experience the refresh problems my self. But the code is very experimental, and still more like a prove of concept. Hopefully i will find time to improve the code in the near future, or maybe somebody else will create his own project from this with more stable code.

Comment only
18 Oct 2010 Nicolas Billard

It's interesting project apart some stability problems.

The following message is often displayed (when i launch webserver first time, it's ok. If i stop it and run it again, it's rain of messages) :

??? Java exception occurred: Address already in use: JVM_Bind
at Method)
at Source)
at Source)
at<init>(Unknown Source)
at<init>(Unknown Source)
Error in ==> JavaTcpServer at 10
serverSocket = ServerSocket(data);
Error in ==> webserver at 33

Other problem, i must refresh sometimes until almost 10 times before the server executes my url.

If you have any suggestion to improve the stability, I'm very interested.

25 Nov 2010 1.1

Fixed Java-Port error due to code crash, and fixed refresh bug by waiting longer for the data. Problems where reported by "Nicolas Billard"

25 Nov 2010 1.2

Now it also works with the "ICE-browser" in Matlab. Also added error information and config options.

29 Nov 2010 1.3

Update for ice-browser

18 Oct 2011 1.4

- Chinese character-set bug (reported by Ernest)
- Try and catch around java connection error caused by IE
- added config variable to function-header

Contact us