Converts a string representation of a color to an RGB triple
Updated 3 Apr 2008

View License

A simple utility to convert a ColorSpec value, represented as a string, to a numeric RGB triple. This function does not use eval. One possible use is for reading a user-entered color value from an editable text box.

str2rgb('yellow') returns [1 1 0]
str2rgb('y') returns [1 1 0]
str2rgb('[1 1 0]') returns [1 1 0]
str2rgb('1 1 0') returns [1 1 0]
str2rgb('[1; 1; 0]') returns [1 1 0]
str2rgb('[0 0.5 0.91]') returns [0 0.5000 0.9100]
str2rgb('purple') returns NaN
str2rgb('[1 2]') returns NaN

Cite As

Kenneth Eaton (2024). str2rgb (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R14SP3
Compatible with any release
Platform Compatibility
Windows macOS Linux
Find more on Characters and Strings in Help Center and MATLAB Answers

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes