以 MATLAB 轉換 OOIBase32 原始檔

本文分享一個 MATLAB 之 m 函數原始檔,可將 OOIBase32 來源之原始文字檔轉換成二個經過三次平滑之一維陣列,並生成一個純文字(.dat)之結果檔案。

OOIBase32 所產生的原始檔,通常長這個樣子。

OOIBase32 Version 2.0.6.5 Data File
++++++++++++++++++++++++++++++++++++
Date: 01-24-2007, 17:39:35
User: Valued Ocean Optics Customer
Spectrometer Serial Number: USB4C02907
Spectrometer Channel: Master
Integration Time (msec): 35
Spectra Averaged: 5
Boxcar Smoothing: 0
Correct for Electrical Dark: Disabled
Time Normalized: Disabled
Dual-beam Reference: Disabled
Reference Channel: Master
Temperature: Not acquired
Spectrometer Type: S4000
ADC Type: USB4000
Number of Pixels in File: 3648
Graph Title: 
>>>>>>Begin Spectral Data<>>>>
178.59 0.000
178.81 0.000
179.02 88.000
179.24 -2350.000
179.46 -14.286
179.67 34.043
179.89 60.000
180.10 -3600.000
180.32 100.000
180.54 1266.667
.
.
.
887.29 110.120
887.46 110.528
887.63 111.647
887.80 109.825
887.97 111.218
888.13 110.559
888.30 109.965
>>>>>>End Spectral Data<>>>>

以下的函式可以將上述原始檔進行轉換(假如已經更改過 OOIBase32 原始檔,那我不保証這個函式能正確運作)。請複製以下原始碼,並儲存為 trans_data.m。

#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    any later version.
#    
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

function [trans_x,trans_y] = trans_data(source_file,extension,begin,inter,endd)
% Load the source_file.extension,
% then smooth the data and generate a 2*4001 array,
% and creat a source_file.dat.
% out put: trans_x and trans_y: the transformed and smoothe data

%load the source file
source_file_ext = [source_file '.' extension];
fid = fopen(source_file_ext, 'r');

% sweap several beginning lines
for n = 1:30
 s1=fgetl(fid);
end

%scan wavelength
mydata=fscanf(fid,'%g',[2 inf]);
fclose(fid);
xx=mydata(1,:);
yy=mydata(2,:);
trans_x=begin:inter:endd;
trans_y=interp1(xx,yy,trans_x,'cubit');

%write source_file.dat
fid=fopen([source_file '.dat'],'w');
fprintf(fid,'%9.9f %9.9f\n',[trans_x;trans_y]);
fclose(fid);

trans_data 函式共有 5 個引數,分別為來源檔之檔名、來源檔之副檔名、起始值、平滑間距及終止值。舉例來說,我取得一個由 OOIBase32 得到的來源檔,名叫 color.Master.transmission ,其中包括許多 x 與 y 之對應值。我希望將該檔內的資料由 x=300 取至 x=700,且平滑間距為 0.1,轉換成新的 x 叫 new_x 且新的 y 叫 new_y。方法如下:將 trans_data.m 與 color.Master.transmission 置於同一個資料夾,再從 MATLAB 中到達該資料夾,並輸入:

[new_x,new_y]=trans_data('color','Master.transmission',300,0.1,700)

即可,而且也會在該資料夾中生成一個 color.dat 檔。由於其中 color 與 Master.transmission 為字串,故需以單引號括起來。若想觀看結果,可在 MATLAB 中輸入:

plot (new_x,new_y)

以觀看新產生的結果。

以上例為例,trans_data 函式的輸出有二個變數(new_x與new_y)及一個輸出文字檔(color.dat)。你可以根據你的需要,利用這二種不同類型的資料再引入其它函式中操作。

請注意。由於 trans_data 函式會將 OOIBase32 原始檔前 30 列去除(個人的經驗中前 30 列包括不需要的文字說明及無意義資料),若有需要,請修改將函式中 for n = 1:30 的 30 修改成其它數值。

如何產生 PDF 文件?

本文說明 PDF 文件之重要性,以及如何產生 PDF 文件。

PDF 文件與其優點

所謂的 PDF 文件,基礎上來說,就是給「印表機」看的文件。只要有相同的 PDF 文件,在印表機看來,都是完全相同的列印內容。也因此,PDF 可以不限平台、不限作業系統、不限原生軟體之下,都有相同的列印格式。

PDF 有許多好處。最大的好處就是可以讓所有的人都毫無偏差地看見同一份文件。過去我們習慣使用 MS office word 所產生的 .doc 檔來分享文件,但在不同的版本的 office 所產生的 .doc,在另一個版本的 office 所開啟之後,格式都會跑掉。而且,誰規定每個人都必須購買 MS office 呢?沒有購買 MS office 的人就沒有權利接受別人生產的文件嗎?在 PDF 的世界,因為 PDF 是給印表機看的檔案,所以不會有這樣的問題。另外,通常 PDF 是不可以再更改的。而且,PDF 也可嵌入字型,以確保文字正確顯示。這些都是 PDF 足以做為分享分件的最大優點。

如何產生 PDF 文件

一般在 Windows 系統上,若使用 MS office 的話,並不能直接生產 PDF 文件。但是解決辦法有許多種:

  • 購買 Adobe 公司的 Acrobat Professional。很貴。
  • 使用其它的印表機外掛程式。我引述「戴嘉賢 (Nelson Tai)」先生所整理的免費軟体。請大家選用。該網頁中所介紹的「CutePDF Writer」、「PrimoPDF」等免費軟體可能是你所需要的。當然也可以自行 google 這些軟體再自行下載。
  • 如果你很熟電腦,阿盤最推薦的辦法是,不要使用 MS office 而改用開放原始碼的 OpenOffice。OpenOffice 可以直接生成 PDF。

如果是使用 Unix-like 作業系統,既然你已經玩 Unix-like,那產生 PDF 應該是小 Case 囉!至於 Mac 使用者,內建有產生 PDF 的程式。