MATLAB File Management n dir Display the names

MATLAB File Management

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

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: diary off: diary on: diary: 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. Suspends diary operation. Turns diary operation back on. Toggles diary state

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

Exporting and Importing Data n There 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.

Storing and Loading Workspace Values n n 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

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

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

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

Importing Results from a Text File n 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 elcen. NS 1940. dat (First copy the elcen. NS 1940. dat into the MATLABWork folder)

M-Files n n 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.

M-Files n 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.

Naming M-Files n n 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 which command. For example, to see whether rqroot already exists, type which rqroot.
![MATLAB functions useful in MFiles Command disp(ans) echo [on|off] input(’prompt’) pause(n) Waitforbuttonpress Description Display MATLAB functions useful in MFiles Command disp(ans) echo [on|off] input(’prompt’) pause(n) Waitforbuttonpress Description Display](http://slidetodoc.com/presentation_image_h2/8dffa66a60161308ccc4f8e10fb3dae9/image-14.jpg)
MATLAB functions useful in MFiles Command disp(ans) echo [on|off] input(’prompt’) pause(n) Waitforbuttonpress Description Display results without identifying variable names Control Command window echoing of script commands 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 until user presses any keyboard key Pause for n seconds Pause until user presses mouse button or keyboard key

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

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 s 1 = x+y; disp(’Value of first quadratic root: ’), disp(s 1); s 2 = x-y; disp(’Value of second quadratic root: ’), disp(s 2);
- Slides: 16