150 likes | 258 Views
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, ...)
E N D
Scientific Programming Using MATLAB, Fall 2011-2012 TIRGUL #9: Formatted strings and files
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
Conversion Characters integer floating point compact string
Escape Characters • Specify non-printing characters
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
Files • Mat-files • Other file types • Low level access to files
.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
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’)
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.
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
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,...)
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.
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.
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.