Exporting Vectors edit page

export writes a list of directions to a text file, one direction per line, with a line of column headings on top. The file it produces is one that vector3d.load reads back, so this is the way to hand directions to another program or to a later session.

plottingConvention.default('y↑→x');

v = vector3d.rand(5);

fname = fullfile(tempdir,'vectors.txt');

Cartesian Coordinates

By default the three coordinates are written.

export(v,fname)

type(fname)
x         y         z
-0.479618   0.86809  -0.12801
0.0893455  0.305014 -0.948148
-0.728476 -0.677832  0.099325
-0.304374  0.943729 -0.129355
-0.104185  0.981723 -0.159264

Spherical Angles

The option 'polar' writes the polar and the azimuth angle instead, in degree, or in radians with the additional option 'radians'.

export(v,fname,'polar')

type(fname)
polar angle azimuth angle
    97.3546       118.921
    161.468       73.6735
    84.2997      -137.062
    97.4323       107.876
    99.1642       96.0578

Additional Columns

Values that belong to the directions - a measured intensity, a weight, a density - are written alongside them by passing a struct. Each field becomes one further column, named after the field.

S.weight = (1:5).'/15;

export(v,fname,S)

type(fname)
x         y         z    weight
-0.479618   0.86809  -0.12801 0.0666667
0.0893455  0.305014 -0.948148  0.133333
-0.728476 -0.677832  0.099325       0.2
-0.304374  0.943729 -0.129355  0.266667
-0.104185  0.981723 -0.159264  0.333333

Reading the File Back

vector3d.load takes the coordinate columns by name and ignores the rest.

vNew = vector3d.load(fname,'ColumnNames',{'x','y','z'});

max(angle(v,vNew)) ./ degree
ans =
   2.9919e-05

The directions come back to within a few \(10^{-5}\) degree. What limits the roundtrip is the six digits written to the file, not the export itself.

delete(fname)

Next

Reading directions from a file is Import. A whole figure, rather than the data behind it, is saved as described in Exporting Figures.