File Exchange

image thumbnail

Matlab 3D figure to 3D (X)HTML

version 1.9 (436 KB) by

Converts 3D objects of a Matlab figure to XHTML embedded X3D file. Interactive 3D website graphics.



View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

This function FIGURE2XHTML converts the 3D objects of a Matlab figure to an in XHTML embedded X3D file. In a modern browser, you can then view your figure interactively because of the great X3DOM Library (Instant 3D the HTML way!
Currently the function supports: Axes, Patch, Line, Surface, Text, Images and Light Objects.

Note: Even volume rendering in your browser is possible, by using:

Browsers supported:
 - Google Chrome 9.x and above
 - Firefox 4.x and above
 - Webkit nightly builds
 - Flash11 needed for Internet explorer (or InstantReality-plugin)

  Note : All input arguments are optional, and sorting doesn't matter
 thus figure2xhtml(); is valid but also figure2xhtml(options,handle);

    filename : Name of the XHTML file (also an X3D file is created).
           When empty or not known a File-Dialog is shown
    handle : Figure handle or axis handle
           When empty or not known the current axis (GCA) is used
    options : A struct with options
      options.output : Produce output files 'x3d', 'xhtml' or 'both' (default)
      options.width : Width of X3D render object in pixels default 500
      options.height : Height of X3D render object in pixels default 500
      options.headlight : Enable Camera head light, boolean true/false
                    (default true)
      options.title : Title of xhtml page, default 'Matlab X3D'
      options.interactive : Make mesh/surface objects clickable in xhtml,
                    boolean true/false (default false)
   figure, hold on; axis equal;
   patch(FV,'facecolor',[1 0 0],'facealpha',0.5);

Comments and Ratings (32)

Very nice dunction! However i tried exporting a colored surf(X,Y,Z,C) which has some NaNs for some of the Z and C values (as there is no data availbale for these grid points. In MATLAB figure, these areas are not visible, but in the x3d they appear with the lowest colormap value. Anyway to change that?

I located a 2014 version of matlab on my computer and for whatever reason in that version, writexhtmlfile() was able to load the -.mat file on this version, successfully generating the html.

Really thrilled about the prospect of this - it's exactly what I've been looking for to create 3D html of a complex line graph to show to customers at work. Unfortunately, I'm having an error reading the x3dom.mat file in writexhtmlfile.m. I'm assuming this is supposed to be generated earlier in the function since it is a -.mat file, but I haven't found the source of the error yet. Did anyone else solve/experience this?

Mehdi Gh

doesn't work with patch objects. Limited to surfaces only

"Error using horzcat
Dimensions of matrices being concatenated are not consistent."

How to get rid of this error? I tried it on a surface. BTW, your example doesn't work on R2016a :(


My problem is solved, to set options you must create

options = struct('output','both','height',500,...); %names in help or in figure2xhtml.m
figure2xhtml(options,h) % "save as" window is opened


Incredible work!! But could you help me, please? I write:

figure2xhtml(h); %I want the dialog

I would like set options.title and options.interactive, but I don't know how I can do it. Could you give an example?


Curtis (view profile)

Works great, but can only be used for surfaces defined by 3 matrices surf(X,Y,Z) or one matrix surf(Z), but not 2 vectors and a matrix surf(x,y,Z)

Error using horzcat
Dimensions of matrices being concatenated are not consistent.

Error in surf2FV (line 13)
    V=[XData(:) YData(:) ZData(:)];

A remarkable work, and it just works !
However, as the exported files are ASCII based, beware of the size of the object to export.

Ben Jordan

Thank you for this. Can you make any suggestions about how to extend your code to allow for an animated series to be played?


Laila (view profile)


mini question: is there anyway to get it to work with elements from an hggroup (such as quiver3?).


Truong (view profile)

I ran on a 3-D figure (created with patches) and got the following error:
??? SWITCH expression must be a scalar or string constant.

Error in ==> addmesh at 142

Error in ==> figure2xhtml>figurex3d at 196

Error in ==> figure2xhtml at 164

The examples provided with the function ran well without errors.


Alex (view profile)

Does this plugin support color images as texture or just grayscale images?


KSSV (view profile)

       Very good work....I have been searching for saving a MATLAB figure interactively into html. Searched and found the MATLAB builder ja which is bit complicated. My further search landed me here. Very good work.
       But I have a problem in this. I ran these files and saved html version in test folder. I tried to open it with chrome and mozilla which are leatest, I get a blank screen showing X3DOM downloading the secene, but never I am getting the interactive image. Flash player is installed.
Why is it? Any solution?


J. Hu

J. Hu (view profile)

Thanks for the awesome work. I have been looking for this kind of program for a long time. I really want to explore it. However, I cannot get it to work on my computer. Running the matlab code has no problem. When I tried to open the xhtml file using the latest IE with adobe 11 installed, the web page just show X3DOM Loading Scene... and it neven show up.

I tried to open the xhtml file using the lastest Chrome also. The progrom says that Matalb X3D not responsive and nothing show up also.

Does anybody has the same problem I had and happen to know what may be the reasons, how to fix it?


very exciting option.
It can be interesting include of your option as apart of publish MATLAB function.
Or may be you know how to do that ?

Dirk-Jan Kroon

Dirk-Jan Kroon (view profile)

The same viewpoint is not supported.

The camera in Matlab is a little bit strange, (almost) orthogonal, allows non-uniform x,y scaling and does some tricks to stay outside the rendered object. Thus porting the viewpoint to X3D gave strange results.


Ben (view profile)

Could I use the same viewpoint as that for the figure?

Dirk-Jan Kroon

Dirk-Jan Kroon (view profile)

Do you get an error with your MRI data?, or know what is the problem?


Nino (view profile)

I test it with the MRI brain of Matlab and is not working, but cheek it with your example and is pretty cool. Thanx for sharing

Dirk-Jan Kroon

Dirk-Jan Kroon (view profile)

The latest version supports options like a title.


Ben (view profile)

Is there a way to specify the title, the header, and the object name? That would be very helpful. Otherwise, one has to modify the xhtml source code every time, which could be tedious...

Give you 4 stars because the tool is cool!


xu (view profile)

Comments are almost completely absent from the code.


Petter (view profile)

Comments are almost completely absent from the code.

Dirk-Jan Kroon

Dirk-Jan Kroon (view profile)

*Sven Körner
Nice tools, Javaview is indeed a very extensive 3D viewer for the web.

I don't know the u3d-file format, but maybe it is possible to include line geometry thus allow axis2lines function.

There are way to many geometry-file formats, hopefully they will merge sometime.

Sven Koerner

Sven Koerner (view profile)

Thank's for update - it works well! For my 3D-HTML version I used javaview - there is the axis option already embedded.(
But in the tool to embded u3d files in pdf - there is actually also no axis-feature ( Maybe I can use your axis2lines function?

Dirk-Jan Kroon

Dirk-Jan Kroon (view profile)

*Sven Körner
I added an example with axis to xhtml in the last update (is probably online tomorrow).

Oliver Woodford

Absolutely brilliant.

Dirk-Jan Kroon

Dirk-Jan Kroon (view profile)

*Sven Körner
Axis are not supported by the x3dom library, but text and lines are.
Thus a solution can be to draw the axis your self using plot3 in Matlab

Sven Koerner

Sven Koerner (view profile)

This is pretty cool.
Is there a way how to get the coordinate system (x,y and z-axis) of a function in the 3D-graph?



Improved Internet Explorer support


Added options


added alphamap support


Added images support.
Added reverse axis support.


Added Line-Marker support


Font-size adjustment


added text support, and example with axis in xhtml.


- Added Surface Texture Support
- Fixed NaN bug in vertex-normals

MATLAB Release
MATLAB 7.12 (R2011a)

Inspired: Export figure to 3D interactive PDF

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

» Watch video