Presentation is loading. Please wait.

Presentation is loading. Please wait.

MATLAB File Management. MATLAB User File Management Matlab provides a group of commands to manage user files. For more information, type help iofun. pwd.

Similar presentations


Presentation on theme: "MATLAB File Management. MATLAB User File Management Matlab provides a group of commands to manage user files. For more information, type help iofun. pwd."— Presentation transcript:

1 MATLAB File Management

2 MATLAB User File Management Matlab provides a group of commands to manage user files. For more information, type help iofun. pwd : Print working directory – displays the full path of the present working directory. cd path : Change to directory (folder) given by path, which can be either a relative or absolute path. dir : Display the names of the directories (folders) and files in the present working directory. what : Display the names of the M-files and MAT-files in the current directory. delete file : Delete file from current directory type file : Display contents of file (text file only).

3 Diary Command The diary commands allows you to record all of the input and displayed output from a Matlab interactive workspace session. The commands include: diary file: Saves all text from the Matlab session, except for the prompts (>>), as text in file, written to the present working directory. If file is not specified, the information is written to the file named diary. diary off : Suspends diary operation. diary on: Turns diary operation back on. diary: Toggles diary state

4 Diary Example Problem: solve for s: s 2 + 5s + 6 = 0 >> diary roots >> a=1; >> b=5; >> c=6; >> x = -b/(2*a); >> y = sqrt(b^2-4*a*c)/(2*a); >> s1 = x+y s1 = -2 >> s2 = x-y s2 = -3 diary off The file roots is written in your current working directory. It can be displayed by the Matlab command type roots.

5 Exporting and Importing Data There are also situations in which you wish to export Matlab data to be operated upon with other programs, or to import data created by other programs. This must be done with text files written with save or read with load.

6 Storing and Loading Workspace Values save Stores workspace values (variable names, sizes, and values), in the binary file matlab.mat in the present working directory save data Stores all workspace values in the file data.mat save data_1 x y Stores only the variables x and y in the file data_1.mat load data_1 Loads the values of the workspace values previously stored in the file data_1.mat

7 Exporting Results to a Text File To write a text file data1.dat in the current working directory containing values of Matlab variables in long e format: save data1.dat –ascii Note that the individual variables will not be identified with labels or separated in any way.

8 Exporting Results to a Text File It is often desirable to write text files containing the values of only a single variable, such as: save data1.dat a –ascii where results are seperated by spaces.

9 Exporting Results to a Text File It is often desirable to write text files containing the values of only a single variable, such as: save data1.dat a –ascii -tab where results are seperated by tabs.

10 Importing Results from a Text File The load command followed by the filename will read the information into an array with the same name as the base name of the data file (extension removed). load elcenNS1940.dat (First copy the elcenNS1940.dat into the MATLAB\Work folder)

11 M-Files For simple problems, entering commands at the Matlab prompt in the Command window is simple and efficient. However, when the number of commands increases, or you want to change the value of one or more variables, reevaluate a number of commands, you will want to prepare a script, which is a sequence of commands written to a file.

12 M-Files By simply typing the script file name at a Matlab prompt, each command in the script file is executed as if it were entered at the prompt.

13 Naming M-Files The name must begin with a letter and may include digits and the underscore character. Do not give a script file the same name as a variable it computes, because Matlab will not be able to execute that script file more than once unless the variable is cleared. Do not give a script.le the same name as a Matlab command or function. You can check to see whether a function already exists by using the the which command. For example, to see whether rqroot already exists, type which rqroot.

14 MATLAB functions useful in M-Files Command Description disp(ans) Display results without identifying variable names echo [on|off] Control Command window echoing of script commands input(’prompt’) Prompt user with text in quotes, accept input until “Enter” is typed keyboard Give control to keyboard temporarily. Type Return to return control to the executing script M-file. pause Pause until user presses any keyboard key pause(n) Pause for n seconds WaitforbuttonpressPause until user presses mouse button or keyboard key

15 Script Example Derive and apply the quadratic equation by first expressing the quadratic polynomial in parametric form, as 2 + bs + c = 0

16 Script Example % rqroots: Quadratic root finding script format compact; % prompt for coefficient input a = input(’Enter quadratic coefficient a: ’); b = input(’Enter quadratic coefficient b: ’); c = input(’Enter quadratic coefficient c: ’); disp(’’) % compute intermediate values x & y x = -b/(2*a); y = sqrt(b^2-4*a*c)/(2*a); % compute and display roots s1 = x+y; disp(’Value of first quadratic root: ’),disp(s1); s2 = x-y; disp(’Value of second quadratic root: ’),disp(s2);


Download ppt "MATLAB File Management. MATLAB User File Management Matlab provides a group of commands to manage user files. For more information, type help iofun. pwd."

Similar presentations


Ads by Google