fprint

8
Contents fprintf Write data to text file Syntax fprintf(fileID,formatSpec,A1,...,An) example nbytes = fprintf(fileID,formatSpec,A1,...,An) fprintf(formatSpec,A1,...,An) example Description example fprintf(fileID,formatSpec,A1,...,An) applies the formatSpec to all elements of arrays A1,...An in column order, and writes the data to a text file. fprintf uses the encoding scheme specified in the call to fopen. nbytes = fprintf(fileID,formatSpec,A1,...,An) additionally returns the number of bytes that fprintf writes. example fprintf(formatSpec,A1,...,An) formats data and displays the results on the screen. Examples Print Literal Text and Array Values Print multiple numeric values and literal text to the screen. A1 = [9.9, 9900]; A2 = [8.8, 7.7 ; ... Página 1 de 8 fprintf 07/06/2015 file:///C:/Program%20Files/MATLAB/R2013a/help/matlab/ref/fprintf.html

Upload: susana-montero

Post on 06-Sep-2015

223 views

Category:

Documents


2 download

DESCRIPTION

print

TRANSCRIPT

  • ContentsfprintfWrite data to text file

    Syntax fprintf(fileID,formatSpec,A1,...,An)

    example nbytes = fprintf(fileID,formatSpec,A1,...,An)

    fprintf(formatSpec,A1,...,An)example

    Descriptionexample

    fprintf(fileID,formatSpec,A1,...,An) applies the formatSpec to all elements of arrays A1,...An in column order, and writes the data to a text file. fprintf uses the encoding scheme specified in the call to fopen.nbytes = fprintf(fileID,formatSpec,A1,...,An)additionally returns the number of bytes that fprintf writes.

    examplefprintf(formatSpec,A1,...,An) formats data and displays the results on the screen.

    ExamplesPrint Literal Text and Array ValuesPrint multiple numeric values and literal text to the screen.

    A1 = [9.9, 9900]; A2 = [8.8, 7.7 ; ...

    Pgina 1 de 8fprintf

    07/06/2015file:///C:/Program%20Files/MATLAB/R2013a/help/matlab/ref/fprintf.html

  • 8800, 7700]; formatSpec = 'X is %4.2f meters or %8.3f mm\n'; fprintf(formatSpec,A1,A2)

    X is 9.90 meters or 9900.000 mm X is 8.80 meters or 8800.000 mm X is 7.70 meters or 7700.000 mm

    Print Double-Precision Values as IntegersExplicitly convert double-precision values with fractions to integer values.

    a = [1.02, 3.04, 5.06]; fprintf('%d\n',round(a));

    1 3 5

    Write Table to Text FileWrite a short table of the exponential function to a text file called exp.txt.

    x = 0:.1:1; A = [x; exp(x)];

    fileID = fopen('exp.txt','w'); fprintf(fileID,'%6s %12s\n','x','exp(x)'); fprintf(fileID,'%6.2f %12.8f\n',A); fclose(fileID);

    The first call to fprintf prints header text x and exp(x), and the second call prints the values from variable A.If you plan to read the file with Microsoft Notepad, use '\r\n' instead of '\n' to move to a new line. For example, replace the calls to fprintf with the following:

    Pgina 2 de 8fprintf

    07/06/2015file:///C:/Program%20Files/MATLAB/R2013a/help/matlab/ref/fprintf.html

  • fprintf(fileID,'%6s %12s\r\n','x','exp(x)'); fprintf(fileID,'%6.2f %12.8f\r\n',A);

    MATLAB import functions, all UNIX applications, and Microsoft Word and WordPad recognize '\n' as a newline indicator.View the contents of the file with the type command.

    type exp.txt

    Display Hyperlinks in Command WindowDisplay a hyperlink (The MathWorks Web Site) on the screen.

    site = 'http://www.mathworks.com'; title = 'The MathWorks Web Site';

    fprintf('%s\n',site,title)

    Exporting a Cell Array to a Text File Appending or Overwriting Existing Files

    Input ArgumentsfileID File identifier1 (default) | 2 | scalarFile identifier, specified as one of the following: A file identifier obtained from fopen. 1 for standard output (the screen). 2 for standard error.Data Types: double

    formatSpec Format of the output fields

    Pgina 3 de 8fprintf

    07/06/2015file:///C:/Program%20Files/MATLAB/R2013a/help/matlab/ref/fprintf.html

  • stringFormat of the output fields, specified as a string.The string can include a percent sign followed by a conversion character. The following table lists the available conversion characters and subtypes.

    Value Type Conversion DetailsInteger, signed %d or %i Base 10

    Integer, unsigned %u Base 10

    %o Base 8 (octal)

    %x Base 16 (hexadecimal), lowercase letters af

    %X Same as %x, uppercase letters AF

    Floating-point number %f Fixed-point notation

    %e Exponential notation, such as 3.141593e+00

    %E Same as %e, but uppercase, such as 3.141593E+00

    %g The more compact of %e or %f, with no trailing zeros

    %G The more compact of %E or %f, with no trailing zeros

    %bx or %bX%bo%bu

    Double-precision hexadecimal, octal, or decimal value

    Pgina 4 de 8fprintf

    07/06/2015file:///C:/Program%20Files/MATLAB/R2013a/help/matlab/ref/fprintf.html

  • Value Type Conversion DetailsExample: %bx prints pi as 400921fb54442d18

    %tx or %tX%to%tu

    Single-precision hexadecimal, octal, or decimal valueExample: %tx prints pi as 40490fdb

    Characters %c Single character

    %s String of characters

    The string can include optional operators, which appear in the following order (includes spaces for clarity):

    Optional operators include: Identifier

    Order for processing inputs. Use the syntax n$, where n represents the position of the value in the input list.For example, '%3$s %2$s %1$s %2$s' prints inputs 'A', 'B', 'C' as follows: C B A B.

    Flags

    '' Left-justify. Example: %-5.2f

    '+' Print sign character (+) for positive values. Example: %+5.2f

    Pgina 5 de 8fprintf

    07/06/2015file:///C:/Program%20Files/MATLAB/R2013a/help/matlab/ref/fprintf.html

  • ' ' Pad to field width with spaces before the value. Example: % 5.2f

    '0' Pad to field width with zeros. Example: %05.2f

    '#' Modify selected numeric conversions: For %o, %x, or %X, print 0, 0x, or 0X prefix. For %f, %e, or %E, print decimal point even when precision is

    0. For %g or %G, do not remove trailing zeros or decimal point.Example: %#5.0f

    Field widthMinimum number of characters to print. Can be a number, or an asterisk (*) to refer to an argument in the input list. For example, the input list ('%12d', intmax) is equivalent to ('%*d', 12, intmax).

    Precision

    For %f, %e, or %E: Number of digits to the right of the decimal point.Example: '%6.4f' prints pi as '3.1416'

    For %g or %G Number of significant digits.Example: '%6.4g' prints pi as ' 3.142'

    Can be a number, or an asterisk (*) to refer to an argument in the input list. For example, the input list ('%6.4f', pi) is equivalent to ('%*.*f', 6, 4, pi).

    The string can also include combinations of the following:

    Pgina 6 de 8fprintf

    07/06/2015file:///C:/Program%20Files/MATLAB/R2013a/help/matlab/ref/fprintf.html

  • Literal text to print. To print a single quotation mark, include '' in formatSpec.

    Control characters, including:

    %% Percent character

    \\ Backslash

    \a Alarm

    \b Backspace

    \f Form feed

    \n New line

    \r Carriage return

    \t Horizontal tab

    \v Vertical tab

    \xN Character whose ASCII code is the hexadecimal number, N

    \N Character whose ASCII code is the octal number, N

    The following limitations apply to conversions: Numeric conversions print only the real component of complex numbers. If you specify a conversion that does not fit the data, such as a string conversion

    for a numeric value, MATLAB overrides the specified conversion, and uses %e. If you apply a string conversion (%s) to integer values, MATLAB converts values

    that correspond to valid character codes to characters. For example, '%s'converts [65 66 67] to ABC.

    Pgina 7 de 8fprintf

    07/06/2015file:///C:/Program%20Files/MATLAB/R2013a/help/matlab/ref/fprintf.html

  • A1,...,An Numeric or character arraysscalar | vector | matrix | multidimensional arrayNumeric or character arrays, specified as a scalar, vector, matrix, or multidimensional array.Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char

    More AboutTips Format specifiers for the reading functions sscanf and fscanf differ from the

    formats for the writing functions sprintf and fprintf. The reading functions do not support a precision field. The width field specifies a minimum for writing but a maximum for reading.

    Formatting Strings

    References[1] Kernighan, B. W., and D. M. Ritchie, The C Programming Language, Second Edition, Prentice-Hall, Inc., 1988.

    [2] ANSI specification X3.159-1989: "Programming Language C," ANSI, 1430 Broadway, New York, NY 10018.

    See Alsodisp | fclose | ferror | fopen | fread | fscanf | fseek | ftell | fwrite | sprintf

    Was this topic helpful? Yes No

    Pgina 8 de 8fprintf

    07/06/2015file:///C:/Program%20Files/MATLAB/R2013a/help/matlab/ref/fprintf.html