fprintf_fscanf_fopen - Matlab fprintf/fscanf/fopen...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Matlab fprintf/fscanf/fopen Preceding the disp( ) function with one of the format commands (ie. format long or format short e) gives some control over how the value will be displayed, but often not enough. It is also pretty tiresome to have to explicitly convert a number to a character string (num2str(pi)) in order to combine it with character labeling. Matlab does, of course, have an "output" function that gives us more control. It is the fprintf function: fprintf(format,A); causes the value(s) in the variable A to appear in the command window according to the format . format is a string containing "conversion specifications", and/or labeling characters, and/or non-printing escape characters such as new line (\n) or tab (\t). For example: fprintf( 'pi to 3 decimals is %5.3f\n' ,pi); produces in the command window pi to 3 decimals is 3.142 The conversion specification here is %5.3f which is used to convert the numerical value of pi into characters (like num2str) but specifically asking for a minimum total number of characters (called the field width) of 5, with 3 places to the right of the decimal. If you ask for a larger field width than required by the number, you get leading blanks: fprintf( '%7.3f\n' ,pi); produces in the command window 3.142 which has two leading blanks (the number is "right-justified" in the field) On the other hand, if you do not specify a large enough field width, it will automatically expand to the minimum necessary: fprintf( '%5.3f\n' ,100*pi) produces in the command window 314.159 Note that the values are displayed rounded to the specified precision, not truncated. (This is an error in Appendix C of your
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/15/2011 for the course ECE 1331 taught by Professor Staff during the Spring '08 term at University of Houston.

Page1 / 3

fprintf_fscanf_fopen - Matlab fprintf/fscanf/fopen...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online