23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Characters and Strings<br />

Function Description Example<br />

dec2hex<br />

dec2bin<br />

dec2base<br />

Convert a positive integer to a character type of<br />

hexadecimal base.<br />

Convert a positive integer to a character type of<br />

binary base.<br />

Convert a positive integer to a character type of<br />

any base from 2 through 36.<br />

[72 105] → '48 69'<br />

[72 105] → '1001000<br />

1101001'<br />

[72 105] → '110<br />

151' (base set to 8)<br />

Converting to a Character Equivalent<br />

The char function converts integers to Unicode character codes and returns a<br />

string composed of the equivalent characters:<br />

x = [77 65 84 76 65 66];<br />

char(x)<br />

ans =<br />

<strong>MATLAB</strong><br />

Converting to a String of Numbers<br />

The int2str, num2str, andmat2str functions convert numeric values to<br />

strings where each character represents a separate digit of the input value.<br />

The int2str and num2str functions are often useful for labeling plots. For<br />

example, the following lines use num2str to prepare automated labels for the<br />

x-axis of a plot:<br />

function plotlabel(x, y)<br />

plot(x, y)<br />

str1 = num2str(min(x));<br />

str2 = num2str(max(x));<br />

out = ['Value of f from ' str1 ' to ' str2];<br />

xlabel(out);<br />

Converting to a Specific Radix<br />

Another class of conversion functions changes numeric values into strings<br />

representing a decimal value in another base, such as binary or hexadecimal<br />

representation. This includes dec2hex, dec2bin, anddec2base.<br />

2-61

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!