Contents Previous Next Subchapters

Lining Up Data Fields In A Character Matrix
Syntax [data] = align(textseparatorwidth)
[data] = align(textseparatorwidthadjust)
[datawidth] = align(textseparatorwidth)
See Also read , sort , atod , char2dbl , find2 , strreplace

Description
Returns the character matrix data which contains the information in text with fields aligned to certain columns. The character row vector separator specifies a list of characters that separate fields in the matrix text. The integer row vector width can be either an input value or a return value determined by align. It specifies the number of columns in each field and the number of fields. If present, adjust must be "right" and specifies that the resulting values should be right, instead of left, adjusted.

Example
The align function fills the end of each field with spaces to obtain the specified width. Suppose you had recorded the following weather conditions:
     city             temp.     conditions
     Chicago           65         windy
     Atlanta           80         sunny
     WashingtonD.C.  72         humid
     Seattle           63          rain
     Los Angeles       77         cloudy
If you enter
     clear
     text = { ...
          "Chicago;65;windy",          ...
          "Atlanta;80;sunny",          ...
          "Washington, D.C.;72;humid", ...
          "Seattle;63;rain",           ...
          "Los Angeles;77;cloudy"      ...
     }
     separator   = ";"
     [data, width] = align(text, separator)
     print data
O-Matrix will respond
     Chicago          65 windy
     Atlanta          80 sunny
     Washington, D.C. 72 humid
     Seattle          63 rain
     Los Angeles      77 cloudy
If you continue by entering
     print width
O-Matrix will respond
     [ 17 , 3 , 7 ]
If a field is longer than the specified width, it is truncated. If you continue the last example by entering
     width = [10, 5, 15]
     align(text, separator, width)
O-Matrix will respond
     Chicago   65   windy
     Atlanta   80   sunny
     Washington72   humid
     Seattle   63   rain
     Los Angele77   cloudy


If a row of text contains two or more separators with no other character between, they only count as one field separator. Thus multiple spaces can be used to separate fields. In addition, leading and trailing blanks are deleted from each field. If you enter
     text = { ...
          " a;   b   c ", ...
          "d   ;e   f  "  ...
     }
     separator  = "; "
     width      = [5, 5, 5]
     align(text, separator, width)
O-Matrix will reply
     a    b    c
     d    e    f


By default align uses left adjustment of the fields (as in the previous example). The right adjustment option fills the beginning of each field with spaces to obtain the specified width. If you enter
     format int "5"
     x          = {[1, 2, 3, 4], [5, 6, 7, 8]}
     title      = "one;two;three;four"
     separator  = ";"
     width      = [5, 6, 6, 6]
     adjust     = "right"
     header     = align(title, separator, width, adjust)
     write("screen", header)
     write("screen", x)
O-Matrix will respond
       one   two three  four
         1     2     3     4
         5     6     7     8
Note that field widths for the second, third, and fourth columns are 6 because an extra space is printed between the elements on each row.