Prev: axis-pos.html Up Next: fft.html
Units

Practical Guide:
double f = 35*unit::MHz;
double T = 1/f;
double lambda = cons::c * T;
cerr<<"Wavelength: "<<lambda/unit::cm<<" cm"<<endl;

plot(_1,_1).p1range(10*unit::MHz, 100*unit::MHz);
set::xunit("MHz");

Using units for calculations

One very often wants to work with physical quantities, which have units. Using powers of ten might often be confusing. Other, non-standard units can be even more tricky to remember.

The namespace unit defines constant numbers representing physical units, in the SI system. For example, unit::ms=0.001 (millisecond), or unit::mus=1e-6 (microsecond). To get the SI-value of a quantity with a unit, one simply needs to multiply a number with a unit: 24*unit::ms. To print the value of a dimensioned quantity in a certain unit, simply divide it by the unit:

cerr<<"Elapsed time: "<<t/unit::ms<<" ms"<<endl;

Setting a unit for an axis

One can set a unit for a given axis. The displayed values along that axis will be be in that unit, and the unit indication (by default [unitname], in square brackets) will be appended to the axis title.

axis::unit(const var &u, const var &format="[$%s$]");
This member function of the axis class sets the unit for the axis. The second argument, format controls the format of the appended unit-indication to the title. %s is replaced by the unit symbol. This example below sets the unit of the x1 axis of the current frame:
frame::current().x1axis()->unit("MHz");
The shortcut for this (and similar) operations:
set::xunit(const var &u, const var &format="[$%s$]");
set::x1unit(const var &u, const var &format="[$%s$]");
set::yunit(const var &u, const var &format="[$%s$]");
set::y1unit(const var &u, const var &format="[$%s$]");
set::x2unit(const var &u, const var &format="[$%s$]");
set::y2unit(const var &u, const var &format="[$%s$]");

The format of the first argument can be


Source files
   units.h
   units.cc

Prev: axis-pos.html Up Next: fft.html