LECTURE_10_CS113_2010 - CS113 forEngineers...

Info icon This preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
CS113 Introduction to Computing for Engineers Formatting Data, Cell Arrays and Case Structure
Image of page 1

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

View Full Document Right Arrow Icon
Formatting Data (#’s and/or strings) The fprintf( format , data ) function %d integer %5d Integer, minimum width = 5 %f floating point format %4f Floating point, minimum width = 4 %6.3f Float, min width = 6, with 2 digits past decimal point. %e exponential format \n new line character \t tab character
Image of page 2
MATLAB Basics: Displaying Data fprintf( 'Result is %d', 3 ); Result is 3 fprintf( 'Area of a circle with radius %d is %f\n', 3, pi*3^2 ); Area of a circle with radius 3 is 28.274334 x = 5; fprintf( 'x = %3d\n', x ); x = 5 x = pi; fprintf( 'x = %0.2f\n', x ); x = 3.14 fprintf( 'x = %6.2f\n', x ); x = 3.14 fprintf( 'x = %d\ny = %d\n', 3, 13 ); x = 3 y = 13
Image of page 3

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

View Full Document Right Arrow Icon
Opening Files fid = fopen ( filename, permission ) opens the file filename in the mode specified by permission fid is the file id (a positive integer) that is assigned to the file by MATLAB fid is used for all reading, writing and control operations on that file file id 1 is the standard output device and file id 2 is the standard error device fid will contain ‐1 if the file could not be opened
Image of page 4
Opening Files Permission can be: ‘r’: open file for reading (default) ‘w’: open file, or create a new file, for writing; discard existing contents, if any ‘a’: open file, or create a new file, for writing; append data to the end of the file ‘r+’: open file for reading and writing ‘w+’: open file, or create a new file, for reading and writing; discard existing contents, if any ‘a+’: open file, or create a new file, for reading and writing; append data to the end of the file Add ‘t’ to the permission string for a text file
Image of page 5

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

View Full Document Right Arrow Icon
Opening Files Examples: fid = fopen( ‘example.dat’, ‘r’ ) opens a binary file for input fid = fopen( ‘example.dat’, ‘wt’ ) opens a text file for output (if example.dat already exists, it will be deleted) fid = fopen( ‘example.dat’, ‘at’ ) opens a text file for output (if example.dat already exists, new data will be appended to the end)
Image of page 6
Closing Files status = fclose ( fid ) closes the file with file id fid If the closing operation is successful, status will be 0
Image of page 7

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

View Full Document Right Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern