1 / 15

Scientific Programming Using MATLAB, Fall 2011-2012

Scientific Programming Using MATLAB, Fall 2011-2012. TIRGUL #9: Formatted strings and files. String formatting. Useful for combining text with variables. sprintf – print f ormatted data to s tring fprintf – print formatted data to file Syntax: str = sprintf(format, A, ...)

nerys
Télécharger la présentation

Scientific Programming Using MATLAB, Fall 2011-2012

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Scientific Programming Using MATLAB, Fall 2011-2012 TIRGUL #9: Formatted strings and files

  2. String formatting • Useful for combining text with variables. • sprintf – printformatted data to string • fprintf – print formatted data to file • Syntax: • str = sprintf(format, A, ...) • When combining variables in our formatted string, we need to specify the type of the variable. We do this using conversion characters

  3. Conversion Characters integer floating point compact string

  4. Escape Characters • Specify non-printing characters

  5. Example: • Variables: • name = ‘Noa’ • height = 1.65 • sprintf(‘My name is %s\nmy height is %.2f’, name, height) • Outputs: My name is Noa my height is 1.65 More examples: stringFormats.m Take 2 numbers after decimal point

  6. Files • Mat-files • Other file types • Low level access to files

  7. .mat files • Files that contain data in a format MATLAB understands best • Data = variables holding values • Basic interaction with mat-files: • save(‘fileName’, var1, var2…) • save(‘fileName’)  saves all variables in workspace to fileName • load(‘fileName’, var1, var2…) • load(‘fileName’)  loads all variables from fileName to workspace

  8. Working with mat-files • Getting a list of variables in a file: • who(‘–file’, ‘fileName’) • whos(‘–file’, ‘fileName’) • Finding the path of a file: • which(‘fileName’)

  9. Working with other file types • Often we need to interact with other file types • Excel spreadsheet • Image • Text files • XML file • CSV • etc. • MATLAB has a collection of functions that handle different file types.

  10. Low level access to files • First thing to do is open the file (for reading and writing) • Use fopen • Syntax: fid = fopen(‘filename’, permission) • fid = file identifier. ‘fid’ is an integer. Starts with 3 • -1 is failure, 1 and 2 are standard output/error • permission: • Note: add an extension to your file name • Example: filename = myFile.txt

  11. Interaction with files • Reading • fscanf - read formatted data from file • data = fscanf(fid,format) • fgetl – get line from file • Writing • fprintf - write formatted data to file • fprintf(fid,format,data,...)

  12. Closing a file • Close files using fclose • Syntax: status = fclose(fid) • or: • fclose(‘all’) • Status = 0 if file is closed successfully, -1 otherwise • This is also a mandatory step – otherwise the file stays open for changes. • Always make sure to have an fclose after an fopen.

  13. Practice • Write a function called ‘printGrades’, that gets grades of students and prints them to a file. • Inputs: • studentNames – cell array of names • grades – a vector of corresponding grades • fileName – a string that specifies the name of the file to be created • Output: • The function should also display a message, telling whether the file has been created correctly. The message should include the file name. • Example: The file ‘myFile’ was created and saved correctly • The function prints the names and grades to the file using formatted strings in the following manner: Final grade for Rona is 91 Final grade for Erez is 100 etc.

  14. More practice (from final quiz 2011) • Write a function called xlsGraph that reads data from an Excel file and plots a graph according to this data. • Note: You may use the MATLAB function ‘mean’ in your code. • The function should get one input argument – a string which is the name of an Excel file. You can assume that the Excel file will contain data arranged in a single table such that the first row and the first column in the table are texts, and all other cells contain numbers [the cell in the (1,1) location is empty]. • Your function should do the following: • Read the data from the Excel file (both the numeric and the text data). • Calculate two vectors: the mean of the numbers in the columns of the data matrix and the mean of the numbers in the rows of the matrix. • Create a new figure with two subplots (arranged in one column), that will graphically display the following: • In the top subplot use the 'bar' command to plot the means of the columns • In the bottom subplot use the 'bar' command to display the means of the rows. Add appropriate titles to both subplots. • BONUS: Set the texts appearing at the first row/column of the data table in the Excel file as the tick-labels of the x-axis.

More Related