23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

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.

2 Data Types<br />

• “Setting Field Width and Precision” on page 2-52<br />

• “Restrictions for Using Identifiers” on page 2-55<br />

Note The examples in this section of the documentation use only the sprintf<br />

function to demonstrate how string formatting works. However, you can run<br />

the examples using the fprintf, warning, anderror functionsaswell.<br />

The Format String<br />

The first input argument in the sprintf statement shown above is the format<br />

string:<br />

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

The string argument can include ordinary text, formatting operators and, in<br />

some cases, special characters. The formatting operators for this particular<br />

string are: %s, %d, %d, %d, and%.2f.<br />

Following the string argument are five additional input arguments, one for<br />

each of the formatting operators in the string:<br />

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

When <strong>MATLAB</strong> processes the format string, it replaces each operator with<br />

one of these input values.<br />

Special Characters. Special characters are a part of the text in the string.<br />

But, because they cannot be entered as ordinary text, they require a unique<br />

character sequence to represent them. Use any of the following character<br />

sequences to insert special characters into the output string.<br />

To Insert . . . Use . . .<br />

Backspace \b<br />

Form feed \f<br />

New line \n<br />

Carriage return \r<br />

2-44

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

Saved successfully!

Ooh no, something went wrong!