Vector to colon notation

Converts a vector into a string with its MATLAB colon notation (single resolution).

You are now following this Submission

Usage: strvec = vect2colon(vec, options)

Converts a vector into a string using MATLAB colon notation (single resolution).

Options are:

'Delimiter' - 'on','yes','off','no' {default on} : Including square brackets []
(or curly brackets {} when output's dimensions are not consistent)
'auto' by default.
'Sort' - 'on','yes','off','no' {default off} : Sort elements in ascending order

'Class' - MATLAB classes

'Repeat' - 'on','yes','off','no' {default on} : Keep repeated elements

MATLAB colon notation is a compact way to refer to ranges of matrix elements.
It is often used in copy operations and in the creation of vectors and matrices.
Colon notation can be used to create a vector as follows
x = xbegin:dx:xend
or
x2 = xbegin:xend
where xbegin and xend are the range of values covered by elements of the x
vector, and dx is the (optional) increment. If dx is omitted a value of 1
(unit increment) is used. The numbers xbegin, dx, and xend need not be
integers.

Example 1:
>> x = [ 50 1000 1100 1200 2 3 4 5 6 10 20 30 40]
>> vect2colon(x)

ans =

[ 50 1000:100:1200 2:6 10:10:40]

or

>> vect2colon(x, 'Sort', 'on')

ans =

[ 2:6 10:10:50 1000:100:1200]

Example 2:
>> x = [ 2 3 4 5 6 10 10.1 10.2 10.3 10.4 1000 1100 1200]
>> s = vect2colon(x)

s =

[ 2:6 10:0.1:10.4 1000:100:1200 ]

Example 3:
>> x = [ 2 3 4 5 6 10 1000 1100 1200 10.1 10.2 10.3 10.4]
>> s = vect2colon(x, 'Delimiter', 'no')

s =

2:6 10 1000:100:1200 10.1:0.1:10.4

See also: mat2colon, eval, str2num

Feedback is appreciated.

Cite As

Javier Lopez-Calderon (2026). Vector to colon notation (https://www.mathworks.com/matlabcentral/fileexchange/22076-vector-to-colon-notation), MATLAB Central File Exchange. Retrieved .

General Information

MATLAB Release Compatibility

  • Compatible with any release

Platform Compatibility

  • Windows
  • macOS
  • Linux
Version Published Release Notes Action
1.6.0.0

fixed bug where the increment is negative
Checks numeric input
Allows to keep repeated values
mat2colon function was implemented (see submission)

1.3.0.0

Include options: 'DelimiterEnable' & 'Sort'

1.2.0.0

P1:Bug, two elements appeared without square brackets ([]).
R1:Fixed JLC, 11/29/2008

1.1.0.0

Description was fixed.

1.0.0.0