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
path
can be used to either display or modify the search path of MATLAB. Its functionality in MATLAB is similar to the functionality of environment variablePATH
for DOS shell in Windows systems, and the use of them is also similar.addpath
can 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 useaddpath
together withgenpath
which can generate path strings for a folder and all its subfolders. To remove an existing path from the search path, you can usermpath
. -
Function
what
can list the path for the current folder, and also files and folders relevant to MATLAB. -
Function
dlmread
is helpful to read delimited files (e.g. Excel files), anddlmwrite
is helpful to write delimited files. However, be careful withdlmwrite
when you want to write high accuracy data into a file, because by defaultdlmwrite
keeps only a few digits. -
To read data from or write data into a file, you must first use function
fopen
to open it to get a file pointer, and then you can use all different kinds of ways to read from (e.g.fread
,dlmread
,fscanf
andtextread
) or write into a file (e.g.fwrite
,dlmwrite
,fprintf
). If you want to access a file randomly, you can usefseek
to move the file point to a specific position. Never forget to close the file pointer using functionfclose
after 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
Profile
button underDesktop
menu. 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 withtic
andtoc
. For example,tic; f(); toc
measures the time of running the codef();
. The third way is to record the time manually using the functionnow
and then use the functionetime
to calculate the elapsed time. The last ways is similar to the third way, but you record the cpu time using functioncputime
instead of the system time. -
pwd
can print out the current work directory of MATLAB, which is similar to thepwd
command 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,pwd
be change to the current working directory. Note that the current working directory is always in the search path of MATLAB. -
You can use
save
to save MATLAB workspace or selected variables into a file andload
to 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
ls
ordir
, which is same to Linux terminal command. -
You can use
exit
orquit
to 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.m
and place the file into the search path of MATLAB or into the current folder. -
To remove files or graphics objects, you can use
delete
. -
fileattrib
can get and set attributes of files and folders in MATLAB.