Presentation is loading. Please wait.

Presentation is loading. Please wait.

More File Input and Output

Similar presentations


Presentation on theme: "More File Input and Output"— Presentation transcript:

1 More File Input and Output
MATLAB Programming More File Input and Output Hello, and welcome to another episode of the Software Carpentry lecture on MATLAB programming. In this episode, we will talk about printing formatted output to the screen and exporting data to a file. Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See for more information. 1

2 Programming tip: all status outputs should be optional.
Why print to the screen? Debugging. Status updates. Auditing. Programming tip: iterative algorithms that might take a long time should have a status printout. Programming tip: all status outputs should be optional. Before we starting printing to the screen, it is important to figure out what kind of output is appropriate to print to the screen. One common practice is to print intermediate status messages in functions that run for a long time. A good status message can help the use abort a program if it is not performing correctly. 2

3 Power iteration algorithm – find largest eigenvalue.
Large, sparse matrices Google’s PageRank bk+1 = A bk / || A bk || Functions that iterate an unknown number of times often have status messages. An example of such a function is the power iteration algorithm, which finds the largest eigenpair of a matrix. It is often used on large, sparse matrices such as the matrix of weights used by Google’s PageRank algorithm. The basic algorithm is embodied in the red equation, which is just a matrix multiplication and division by a scalar 3

4 Compute power iteration: stop_criteria = 1e-5; b = zeros(length(A));
b_new = ones(length(A)); while norm(b-b_new,1) > stop_criteria b = b_new; b_new = A * b / norm(A * b); end Here is a piece of MATLAB code that computes the power iteration of a matrix A. Note that we loop over the iteration until successive approximations b and b_new differ by at most 1e-5. 4

5 How do you know if it is still running?
>> A = rand(10000,10000); >> b = power_iteration(A) … wait a while … b = [… …]; How do you know if it is still running? If we define a large matrix and compute the power iteration, there is no guarantee on how long the algorithm will run. There are several things that can go wrong, including a coding error or floating point error that leads to an infinite loop. 5

6 while norm(b-b_new,1) > stop_criteria b = b_new;
Poor idea: while norm(b-b_new,1) > stop_criteria b = b_new; b_new = A * b / norm(A * b); norm(b-b_new,1) end One way to monitor the calculation is to compute the stopping criteria and leave off the semicolon. 6

7 while norm(b-b_new,1) > stop_criteria b = b_new;
Poor idea: while norm(b-b_new,1) > stop_criteria b = b_new; b_new = A * b / norm(A * b); norm(b-b_new,1) end No semicolon This is not a good idea, because it prints 4 lines per iteration. If we need to print several status variables per iteration, the output is both long and hard to read. 7

8 while norm(b-b_new,1) > stop_criteria b = b_new;
Better idea: i = 0; while norm(b-b_new,1) > stop_criteria b = b_new; b_new = A * b / norm(A * b); fprintf(‘Iteration %d: %f\n’, i,norm(b-b_new,1); i = i + 1; end A better strategy is to utilize the fprintf function, which prints formatted text to a file or to the standard output stream. 8

9 Iteration 0: 968.382585 Iteration 1: 0.008139 Iteration 2: 0.000151
>> b = power_iteration(A); Iteration 0: Iteration 1: Iteration 2: Iteration 3: >> Clearly, this output is cleaner and shorter than it would be if we had just left off a semi-colon. 9

10 fprintf(‘Iteration %d: %f’,i,norm(…));
fprintf(format, value1, value2, …) fprintf(file_handle, format, value1, value2, …) Print formatted text. format: any string % is a special character number of values corresponds to number of %. fprintf(‘Iteration %d: %f’,i,norm(…)); fprintf is mirrored on the printf statement from C or FORTRAN. The important input is format, which is a string that contains special escape sequences that start with a percent sign. Each percent sign corresponds to another parameter that will be printed at the specified location. 10

11 fprintf(‘Iteration %d: %f’,i,norm(…));
fprintf(format, value1, value2, …) Print formatted text. format: any string % is a special character number of values corresponds to number of %. fprintf(‘Iteration %d: %f’,i,norm(…)); The numbers and digits that follow a percent are hints on how to format the number. For instance, %d means to interpret the corresponding value as an integer, and we see that the input i makes sense as an integer. Interpret as integer Value 1 11

12 fprintf(‘Iteration %d: %f’,i,norm(…));
fprintf(format, value1, value2, …) Print formatted text. format: any string % is a special character number of values corresponds to number of %. fprintf(‘Iteration %d: %f’,i,norm(…)); Percent f means to interpret the value as a floating point number, so the norm is printed in floating point format. There is a long list of formatting symbols that is available at the online help for fprintf. Value 2 Interpret as float 12

13 Recording a session with diary: Auditing Data exploration
What did you do? Can you repeat it? Another important use of printed output is for auditing during data exploration. If you are exploring several kinds of analysis, journaling with the diary function can help keep track of what commands were run and what their output was. 13

14 MATLAB stores your recent history, but it doesn’t include the output.
MATLAB always tracks the history of commands that were run from the standard prompt. These can be access from the history screen or by typing the up key in the prompt.

15 History doesn’t store output… Diary stores partial output:
Spot function changes. Spot data changes. History gets us halfway there. It stores what commands we ran, but it does not store a transcript of the actual output. Using diary, we can find changes to output that might be caused by changes to functions or data files. 15

16 Diary record stored in saved_2_26_2011.txt.
>> diary ‘saved_2_16_2011.txt’ >> do stuff … …. >> … >> diary off Diary record stored in saved_2_26_2011.txt. To turn on the diary, type diary and a filename. If you don’t specify a file name, the output is stored in a file called diary. You can turn the diary on and off by typing diary on and diary off. 16

17 Saving data: there is no ‘magic bullet’ like importdata.
Several methods exist: save xlswrite saveas auwrite dlmwrite wavwrite imwrite Unfortunately, there is no single function that can store any kind of data. This should make sense, because it is important to specify how you want to interpret an array or structure. 17

18 1) Save for later retrieval in MATLAB
Saving data: 1) Save for later retrieval in MATLAB - MAT files are smaller and hold all of your variables. - Text files are larger and hold one variable, but they don’t rely on MATLAB. - XML or other formatted options. 2) Export for use by another program If you are saving data that will be reloaded by MATLAB, it is probably best to use MAT files that can store your entire workspace. However, keep in mind that MAT files can not be examined in a text editor, and you are reliant on MATLAB or a third party program to read the files later on. Another option for some data is XML, which is a formatted text file that can store multiple variables. 18

19 1) Save for later retrieval in MATLAB
Saving data: 1) Save for later retrieval in MATLAB 2) Export for use by another program - Use one of the many functions that interpret data as sound, images, video, or other mediums. - Video and sound might require special translators called codecs that are available at MATLAB’s File Central. Many times, it is important to export data as a sound or image or other medium. MATLAB provides functions to translate arrays or structures into many media formats. Some special video or image formats might require codecs to correctly encode a file, and these can be found at MATLAB’s file central. 19

20 Richard T. Guy created by February 2011
The important point is that most data can be interpreted as an array. MATLAB can import data in many different formats, and it provides tools to export data structures back into many media and storage formats. In the next episode, we will look at visualization in MATLAB, which includes tools to create plots and images. February 2011 Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See for more information. 20


Download ppt "More File Input and Output"

Similar presentations


Ads by Google