2009年2月25日 星期三

FORMAT Set output format

利用 format 指令, 可以更改預設的數值顯示方式。
FORMAT with no inputs sets the output format to the default appropriate for the class of the variable. For float variables, the default is FORMAT SHORT.

FORMAT does not affect how MATLAB computations are done. Computations on float variables, namely single or double, are done in appropriate floating point precision, no matter how those variables are displayed. Computations on integer variables are done natively in integer. Integer variables are always displayed to the appropriate number of digits for the class, for example, 3 digits to display the INT8 range -128:127.

FORMAT SHORT and LONG do not affect the display of integer variables.

FORMAT may be used to switch between different output display formats of all float variables as follows:

FORMAT SHORT Scaled fixed point format with 5 digits.

FORMAT LONG Scaled fixed point format with 15 digits for double and 7 digits for single.

FORMAT SHORT E Floating point format with 5 digits.

FORMAT LONG E Floating point format with 15 digits for double and 7 digits for single.

FORMAT SHORT G Best of fixed or floating point format with 5 digits.

FORMAT LONG G Best of fixed or floating point format with 15 digits for double and 7 digits for single.

FORMAT SHORT ENG Engineering format that has at least 5 digits and a power that is a multiple of three

FORMAT LONG ENG Engineering format that has exactly 16 significant digits and a power that is a multiple of three.

FORMAT may be used to switch between different output display formats of all numeric variables as follows:

FORMAT HEX Hexadecimal format.

FORMAT + The symbols +, - and blank are printed for positive, negative and zero elements. Imaginary parts are ignored.

FORMAT BANK Fixed format for dollars and cents.

FORMAT RAT Approximation by ratio of small integers.

FORMAT may be used to affect the spacing in the display of all variables as follows:

FORMAT COMPACT Suppresses extra line-feeds.

FORMAT LOOSE Puts the extra line-feeds back in.

 

控制 Matlab 的顯示方式

1. 顯示或不顯示運算結果
 在同一行撰寫多個 Matlab 指令, 必須用 逗號 (,) 隔開, 如果不想讓運算結果顯示在螢幕上, 就必須在指令的後面加上分號 (;)

2. 指令跨行的控制
 如果指令太長, 無法在一行中把指令撰寫完畢, 可以用跨行字元 (...) "連續三個點", 來表示指令是跨行的。

3. 資料輸出格式的控制: format 指令

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

REPMAT: Replicate and tile an array

REPMAT 指令可以拆解成 replicate matrix, 顧名思義就是複製矩陣的意思,
B = repmat(A,M,N) creates a large matrix B consisting of an M-by-N tiling of copies of A.
將 matrix A 複製 M * N 個, 並以 M 行 N 列的方式排列。舉例來說:
A=[1, 5];
B=repmat(A, 3, 2);
B = [1, 5, 1, 5; 1, 5, 1, 5; 1, 5, 1, 5]

注意: 上述式子中的分號代表換行的意思, 是 MATLAB 矩陣的輸入語法。

2008年9月28日 星期日

IMFINFO:Information about graphics file

透過 IMFINFO 這個指令, 我們可以獲得關於影像檔案的相關資訊。
inf = imfinfo('twins.tif');
觀察 workspace 中的變化,
 
我們發現 IMFINFO 傳回值都放到一個特定型態的結構之中。連按 inf 兩下, 就可以打開 Array Editor 視窗, 觀察 inf 的實際內容。

 
 

SIZE: Size of array

SIZE 這個指令傳回陣列的大小。我們用以下的例子來說明:
x=imread('twins.tif');
s=size(x);
 
 從圖片中我們可以看到 s 的值為 [256, 256, 3], 剛好就是 x 陣列的大小。
 

PIXVAL: Display information about image pixels

PIXVAL('ON') 可以將像素色彩值顯示在圖表視窗(figure window)之中。
figure;
imshow(w);
pixval('on');


我們可以看到 figure 1 底下多了一行反白顯示, 當我們把滑鼠移到圖片上時, 就可以看到圖片上有一個 " + " 的游標, 然後反白顯示列會出現游標的相對應位置與該像素的色彩值。