2008年9月29日 星期一

FOPEN: Open file

FOPEN 這個指令和 C 語言的用法並沒有甚麼不同, 傳回值則是一個整數,
FID = FOPEN(FILENAME)
opens the file FILENAME for read access.
FILENAME can be a MATLABPATH relative partial pathname. If the file is opened for reading and it is not found in the current working directory, FOPEN searches down MATLAB's search path.

FID is a scalar MATLAB integer, called a file identifier. You use the fid as the first argument to other file input/output routines. If FOPEN cannot open the file, it returns -1.
當 MATLAB 無法依照指令找到要開啟的檔案時, 就會傳回 -1, 寫程式時應該特別針對傳回值為 -1 時, 做特別處理。例如:
fid=fopen(filename);
if fid==-1
 error('File does not exist or is not in your MATLAB path.');
end;
就如同 C 語言一樣, FOPEN 指令也可以根據不同的需求, 設定不同的開啟模式。
FID = FOPEN(FILENAME,PERMISSION)
opens the file FILENAME in the mode specified by PERMISSION.
PERMISSION can be:
 'r'  read
 'w'  write (create if necessary)
 'a'  append (create if necessary)
 'r+'  read and write (do not create)
 'w+'  truncate or create for read and write
 'a+'  read and append (create if necessary)
 'W'  write without automatic flushing
 'A'  append without automatic flushing

沒有留言:

張貼留言