16
MATLAB File Management

MATLAB File Management

Embed Size (px)

DESCRIPTION

MATLAB File Management. 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. - PowerPoint PPT Presentation

Citation preview

Page 1: MATLAB File Management

MATLABFile Management

Page 2: MATLAB File Management

MATLAB User File ManagementMatlab 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).

Page 3: MATLAB File Management

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

Page 4: MATLAB File Management

Diary Example

Problem: solve for s: s2 + 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+ys1 =

-2>> s2 = x-ys2 =

-3diary off

The file roots is written in your current working directory. It can be displayed by the Matlab command type roots.

Page 5: MATLAB File Management

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.

Page 6: MATLAB File Management

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

Page 7: MATLAB File Management

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.

Page 8: MATLAB File Management

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.

Page 9: MATLAB File Management

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.

Page 10: MATLAB File Management

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)

Page 11: MATLAB File Management

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.

Page 12: MATLAB File Management

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.

Page 13: MATLAB File Management

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.

Page 14: MATLAB File Management

MATLAB functions useful in M-FilesCommand Descriptiondisp(ans) Display results without identifying variable namesecho [on|off] Control Command window echoing of script

commandsinput(’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 keypause(n) Pause for n secondsWaitforbuttonpress Pause until user presses mouse button or

keyboard key

Page 15: MATLAB File Management

Script Example

Derive and apply the quadratic equation by first expressing the quadratic polynomial in parametric form,

as2 + bs + c = 0

Page 16: MATLAB File Management

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