image thumbnail

read and write CAD Data files using matlab (DX format)

version (4.75 KB) by Evan
read and write CAD data in a DX format


Updated 05 Aug 2013

View License

CAD data files in a DX format are a straightforward and useful way to interpret and communicate 3D data among programs. This pair of programs reads and writes these files to allow users to interface matlab code with such programs.

-- example usage: output a spherical gradient 10 angstroms in radius --

[X Y Z] = meshgrid(-10:10); % make a 3D gradient
input.densityMatrix = ((X.^2 + Y.^2 + Z.^2).^0.5); % construct 3D space
isosurface(input.densityMatrix); % plot in matlab
mat2dx(input); % output as DX file

-- example usage: normalize DX output --

DXdata = dx2mat('DXfile.dx'); % interpret DX data
mat3D = DXdata.densityMatrix; % make name convenient
maxValue = max(max(max(mat3D))); % find max value
minValue = min(min(min(mat3D))); % find min value
valueRange = maxValue - minValue; % find range of data
DXdata.densityMatrix = (mat3D-minValue)/valueRange; % normalize and save
DXdata.outfile = 'DXfile_normaized.dx'; % rename output
mat2dx(DXdata); % write to DX file

%% -- mat2dx.m --
% this function outputs a DX file for viewing in programs such as VMD or PyMOL. It uses
% six inputs to function properly (subvariables of "DXdata"). While only
% one is necessary (the 3D matrix called "densityMatrix"), the others shape
% and position the 3D matrix in space. A minimum point and a voxel length
% are required to do this. The minimum point is recorded as the "minX"
% "minY" and "minZ" variables.
% As of July 2012, file output format is as defined on

%% -- dx2mat.m --
% this function reads in a DX file and translates it into a 3D matrix in
% matlab. DX files are file formats for storing 3D data in plaintext, and
% they are readable by PyMOL and VMD. One inputs the DX file path, and the
% program outputs a structure with all the information about the DX

Cite As

Evan (2021). read and write CAD Data files using matlab (DX format) (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2010a
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!