Sei sulla pagina 1di 3

Getting Started in Matlab. By: Benjamin Cotts 1.

The Current Directory and Path: You should be aware of two things in Matlab that are necessary to run any code you write, but are not typically directly related to the Matlab code itself. They are the "Path" and the "Current Directory". The Current Directory is the location where Matlab will begin any and all operations you run. To check the current directory just type: >>pwd at the Matlab command window (this stands for present working directory - similar to linux). In most versions of Matlab, the default directory the ~/MatlabRoot/work directory (where ~/MatlabRoot is the directory where Matlab has been installed). When you run a Matlab .m file Matlab will look in the Current Directory for the file. If it cannot find the file, it will not run until you have switched the Current Directory to be the one where the file is located. This may seem to be an inefficient way to organize the running of files in Matlab, so to streamline the process Matlab also has the "Path". The Path is a list of directories that Matlab will search (after the Current Directory) for the file you are calling. This way, if you know there is a file you will be using repeatedly, you can add that directory to the Path, and Matlab will always know where to find it. To add a directory to the path go to the File menu and select "Set Path". This will open a separate window which shows which directories are already included in the Path. Once you have added a directory to the Path, you need to save the new Path if you want the changes to be permanent. Note here that the order is important. If you have a file with the same name in two different locations. The one which is higher on the Path will be run instead of the other. A method that some people use to keep organized is to create a "work" folder within the Matlab directory and save many commonly used files there. It is also possible to add a specific directory to the path for the time that the Matlab session remains open. this can be done with the command: >>addpath('FULL_DIRECTORY_PATH_NAME')

2. .m Files A .m file (or file with extension-type of .m) is a file which contains matlab commands. Unlike many programming languages, Matlab is an interpreter language meaning that the code does not need to be compiled before use, you can just enter commands in a .m file and then run it (by typing it's name in the command window) and you will get the same results as if you just type each command into the command window itself.

3. Opening/Editing files To open or edit a new file in Matlab you may either go to File>Open or File>New. This will open or create a new .m file for you to edit. This can also be done by typing >>open('FULL_DIRECTORY_PATH_NAME_TO_FILE') or if the Current Directory contains the file you can just type >> open FILENAME or >>open('FILENAME')

4. Commonly used function: matGetVariable.m matGetVariable.m returns a portion of a specified variable from a version 4 .mat file. This is very useful when working with braodband data because using the command >>load('DATA_FILE') will load the entire data file into memory, an d this can cause Matlab to crash because too much data has been loaded. For example, if you have a 30 minute file of data and know that you are interested only in minutes 7-8, you can load in just that portion of the data, or if you do need the whole file you can just load in 1 minute at a time, and iterate through the file using a "for" loop.

5. Help To get help with a specific file you can type >>help COMMAND_NAME into the command window. You can also use the F1 command indicated above. In newer versions of Matlab, pressing the F1 key will bring up a window with the tab "Search Results" You can type in the name of the command you want (or if you do not know the command you can type in what you think it might be) and then browse all the .m files which are related to your search.

6. Useful shortcuts in Matlab. Typical windows shortcuts (or emacs/linux if you prefer). These options can be changed in File>Preferences, under the "Keyboard" tab. o copy/paste/cut/undo/re-do: ctrl-c, ctrl-v, ctrl-x, ctrl-z, ctrl-y F1: Opens the Matlab "Help Browser". F5: Runs the current .m file as if you had typed the name into the command window and run it. F9: Highlight a specific portion of your code and press F9 to evaluate that code (same as if you copied the code into the command window and ran it). F12: Set a Debugging Break Point in a file. Break points will cause the operation of the .m file to stop at that point so that you can help to debug your code. Other Debugging Shortcuts: o F10: Step. This evaluates the next command o F11: Step-In. In a command which involves running another .m file, this will "Step-In" to the file by opening the new file and continuing the debugging at the beginning of that file o "Go Until Cursor": This is the same as adding a new breakpoint, but is often easier to implement. Just put the cursor on the line where you wish the code to continue to and from the menu select: Debug>Go Until Cursor ctrl-r: Insert comment. Highlight a section of code and press ctrl-r to add a comment character (%) to the beginning of each highlighted line. ctrl-t: remove comment. Highlight a section of code and press ctrl-t to remove a comment character (%) to the beginning of each highlighted line. ctrl-i: Auto Indent. Highlight a section of code and press ctrl-i to auto-indent the code. Example tomakethecodeeasiertoread:
if nargin ==1, varNames=matGetVarInfo(fileID); varNames' return end willbecome if nargin ==1, varNames=matGetVarInfo(fileID); varNames' return end

Potrebbero piacerti anche