12.07.2015 Views

Basics of MATLAB and Beyond

Basics of MATLAB and Beyond

Basics of MATLAB and Beyond

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

v = sin((1:3)*pi/6)v =0.5000 0.8660 1.0000>> num2str(v)ans =0.5 0.866031>> q = reshape(1:9,3,3)q =1 4 72 5 83 6 9>> int2str(q)ans =1 4 72 5 83 6 9>> size(ans)ans =3 7You can tell num2str how many digits to display by giving it a secondparameter:>> num2str(pi,2)ans =3.1>> num2str(pi,15)ans =3.14159265358979The second parameter <strong>of</strong> num2str can also specify the format by means<strong>of</strong> C language conversions. These involve the percent character, width<strong>and</strong> precision fields, <strong>and</strong> conversion characters: d, f, e, etc. (see tablebelow). The basic idea is to use a string <strong>of</strong> characters beginning with %to control the formatting. For example, to output five decimal places ina field <strong>of</strong> 12 characters with exponential notation, use:>> num2str(pi,’%12.5e’)ans =3.14159e+00>> num2str(-pi,’%12.5e’)ans =-3.14159e+00>> num2str(pi*1e100,’%12.5e’)c○ 2000 by CRC Press LLC

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

Saved successfully!

Ooh no, something went wrong!