Filesystem in MATLAB
Posted on Dec 14, 2012 in Computer Science
Things under legendu.net/outdated are outdated technologies that the author does not plan to update any more. Please look for better alternatives.
-
Function
pathcan be used to either display or modify the search path of MATLAB. Its functionality in MATLAB is similar to the functionality of environment variablePATHfor DOS shell in Windows systems, and the use of them is also similar.addpathcan be used to add new folders to the search path. It is a more friendly and convenient way to modify the search path thanpath. To add a folder and all its subfolders to the search path, you can useaddpathtogether withgenpathwhich can generate path strings for a folder and all its subfolders. To remove an existing path from the search path, you can usermpath. -
Function
whatcan list the path for the current folder, and also files and folders relevant to MATLAB. -
Function
dlmreadis helpful to read delimited files (e.g. Excel files), anddlmwriteis helpful to write delimited files. However, be careful withdlmwritewhen you want to write high accuracy data into a file, because by defaultdlmwritekeeps only a few digits. -
To read data from or write data into a file, you must first use function
fopento open it to get a file pointer, and then you can use all different kinds of ways to read from (e.g.fread,dlmread,fscanfandtextread) or write into a file (e.g.fwrite,dlmwrite,fprintf). If you want to access a file randomly, you can usefseekto move the file point to a specific position. Never forget to close the file pointer using functionfcloseafter you have done reading or writing. Usually it is much faster to read from or write into a binary file than to read from or write into a text file, and typically a binary file is smaller than a text file that contains the same data. -
There are serveral ways to test the speed of code in MATLAB. The first way is to use the
Profilebutton underDesktopmenu. It is also the recommended way if you work in MATLAB IDE as it tells you which part of your code is the bottle neck of performance. The second way is to surround the code that you want to test withticandtoc. For example,tic; f(); tocmeasures the time of running the codef();. The third way is to record the time manually using the functionnowand then use the functionetimeto calculate the elapsed time. The last ways is similar to the third way, but you record the cpu time using functioncputimeinstead of the system time. -
pwdcan print out the current work directory of MATLAB, which is similar to thepwdcommand in Linux shell. Though it is mutable, you'd better not change it. Because even if you change it, the current working directory will not change. To change the current working directory to a new one, you can use functioncd. Whenever the working directory is changed,pwdbe change to the current working directory. Note that the current working directory is always in the search path of MATLAB. -
You can use
saveto save MATLAB workspace or selected variables into a file andloadto load data from a MAT file to MATLAB workspace. To display variables in the workspace, you can usewhos; to remove some variables from the workspace, you can useclear. -
To display files and sub-folders of a folder, you can use
lsordir, which is same to Linux terminal command. -
You can use
exitorquitto terminate current session of MATLAB, but you'd better save the MATLAB workspace first if necessary. If you want MATLAB to do some tasks before quitting, you can put put the corresponding code into filefinish.mand place the file into the search path of MATLAB or into the current folder. -
To remove files or graphics objects, you can use
delete. -
fileattribcan get and set attributes of files and folders in MATLAB.