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 />

Formatting Strings<br />

The following <strong>MATLAB</strong> functions offer the capability to compose a string that<br />

includes ordinary text and data formatted to your specification:<br />

• sprintf — Write formatted data to an output string<br />

• fprintf — Write formatted data to an output file or the command window<br />

• warning — Display formatted data in a warning message<br />

• error — Display formatted data in an error message and abort<br />

• assert —Generateanerrorwhenaconditionisviolated<br />

The syntax of each of these functions includes formatting operators similar<br />

to those used by the printf function in the C programming language. For<br />

example, %s tells <strong>MATLAB</strong> to interpret an input value as a string, and %d<br />

means to format an integer using decimal notation.<br />

The general formatting syntax for these functions is<br />

functionname(..., format_string, value1, value2, ..., valueN)<br />

where the format_string argument expresses the basic content and<br />

formatting of the final output string, and the value arguments that follow<br />

supply data values to be inserted into the string.<br />

Here is a sample sprintf statement, also showing the resulting output string:<br />

sprintf('The price of %s on %d/%d/%d was $%.2f.', ...<br />

'bread', 7, 1, 2006, 2.49)<br />

ans =<br />

The price of bread on 7/1/2006 was $2.49.<br />

The following sections cover<br />

• “The Format String” on page 2-44<br />

• “Input Value Arguments” on page 2-45<br />

• “The Formatting Operator” on page 2-46<br />

• “Constructing the Formatting Operator” on page 2-47<br />

2-43

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

Saved successfully!

Ooh no, something went wrong!