




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、 基于MATLAB的圖像處理算法綜合應(yīng)用算法開發(fā)(一)實驗類型: 研究(二)實驗?zāi)康模?、培養(yǎng)應(yīng)用MATLAB開發(fā)圖像處理算法的能力。2、掌握開發(fā)綜合性圖像算法的技能與方法。(三)實驗內(nèi)容:彈孔中心位置的圖像處理方法。(四)實驗要求:開發(fā)出算法及程序代碼,并獲得處理結(jié)果。其基本原理是,先對圖像進(jìn)行邊緣檢測,后應(yīng)用數(shù)學(xué)形態(tài)學(xué)的方法將邊緣連接在一起,后填充,應(yīng)用數(shù)學(xué)形態(tài)學(xué)方法對分割彈孔圓形化,再進(jìn)行邊緣檢測獲得彈孔邊緣,最后應(yīng)用最小二乘法擬合圓心的方法,獲得彈孔中心。實驗過程:打開MATLAB軟件,在OPEN中選擇我們在軟件中設(shè)計好的算法程序。clear;close all;I0=imread(p
2、ic.jpg);%I0=rgb2gray(I3);x,y,z=size(I0);%I6=im2bw(I3,0.4);I4=edge(I0,canny,0.1);BW=strel(disk,1);BW2=strel(disk,1);I5=imdilate(I4,BW);I6=imfill(I5,holes);I7=imdilate(I6,BW2);BW3=strel(disk,3);I8=imerode(I7,BW3);I81=imfill(I8,holes);BW4=strel(disk,7);I9=imerode(I81,BW4);BW5=strel(disk,7);I10=imopen(I
3、9,BW5);figure(1),imshow(I0);figure(2),imshow(I4);figure(3),imshow(I5);figure(4),imshow(I6);figure(5),imshow(I7);figure(6),imshow(I8);figure(7),imshow(I9);figure(8),imshow(I10);hold on;boundaries = bwboundaries(I5);mun1,mun2=size(boundaries);for k=1:mun1 b = boundariesk; plot(b(:,2),b(:,1),g,LineWidt
4、h,3); hold on; bt1=b(:,1); bt2=b(:,2); cir_x,cir_y,radis=circlefitting(bt1,bt2); plot(cir_y,cir_x,r*,LineWidth,3); end然后選擇run。軟件則會根據(jù)我們編輯好的算法對目標(biāo)圖片進(jìn)行彈孔輪廓提取,顯示出計算好的圖片如下: 基于MATLAB的圖像處理算法綜合應(yīng)用算法開發(fā)(一)實驗類型: 研究(二)實驗?zāi)康模?、培養(yǎng)應(yīng)用MATLAB開發(fā)圖像處理算法的能力。2、掌握開發(fā)綜合性圖像算法的技能與方法。(三)實驗內(nèi)容:色彩目標(biāo)提取圖像處理應(yīng)用實例。(四)實驗要求:開發(fā)出算法及程序代碼,并獲得處理
5、結(jié)果。應(yīng)用理論:色彩變換色彩變換原理,減色合成法:人眼看到物體的顏色是由于物體反射了物體顏色相同的光。光白光(三原色的混合體)照到物體上時,物體只把它自己的顏色對應(yīng)的光線反射出來,其它的色光被吸收,即從白光中“減”去物體沒有的顏色。這種情況叫減色合成。品紅會吸收綠色,反射紅色光和藍(lán)色光。黃色會吸收白光中的藍(lán)色,反射紅色光和綠色光。青色會吸收白光中的紅色,反射綠色和藍(lán)色。(色度在附近時為紅色,附近為綠色,附近為蘭色)將原圖c3.jpg進(jìn)行如下編輯程序進(jìn)行運算我們可得到色彩變換結(jié)果程序如下:clear;%清除工作區(qū)內(nèi)所有的變量close all;%關(guān)閉所有的figure%global i j y
6、c1 c2 sat hue;Iinp=imread(fig89.bmp);ysize,xsize,zsize=size(Iinp);m_inty=0.5;m_hue=200;m_sat=1;for j=1:ysize for i=1:xsize image_r(j,i)=Iinp(j,i,1); image_g(j,i)=Iinp(j,i,2); image_b(j,i)=Iinp(j,i,3); y(j,i)=double(0.0); c1(j,i)=double(0.0); c2(j,i)=double(0.0); sat(j,i)=double(0.0); hue(j,i)=double
7、(0.0); endend%由RGB變成色差信號y,c1,c2=Rgb_to_yc(image_r,image_g,image_b,xsize,ysize);%由色差信號計算飽和度和色相sat,hue=C_to_SH(c1,c2,xsize,ysize);%亮度飽合度色調(diào)的調(diào)整out_y,out_sat,out_hue=Change_YSH(y,sat,hue,m_inty,m_sat,m_hue,xsize,ysize);%由色調(diào)和飽合度計算色差信號m_c1,m_c2=SH_to_C(out_sat,out_hue,xsize,ysize);%由亮度色差變換RGB信號out_r,out_g,
8、out_b=Yc_to_rgb(out_y,m_c1,m_c2,xsize,ysize);for j=1:ysize for i=1:xsize Iout(j,i,1)=out_r(j,i); Iout(j,i,2)=out_g(j,i); Iout(j,i,3)=out_b(j,i); endendfigure(1),imshow(Iinp);figure(2),imshow(image_r);figure(3),imshow(image_g);figure(4),imshow(image_b);figure(5),imshow(Iout);運行程序后得到如下圖片: 實驗結(jié)果分析:基于YUV
9、彩色系統(tǒng)的灰度圖像著色方法用彩色參考圖著色假定相鄰的像素之間如果有相似的Y 值,那么就會有相似的U 和V 值;視覺上對U 、V 變化的不敏感性,同時假定同類色中顏色的區(qū)別主要是Y 值作用的結(jié)果?;驹瓌t:1)彩色圖像中U、V 值顯著改變的臨界線, 也是Y 值顯著改變的臨界線;2)同類色中相近顏色的區(qū)別在視覺上主要是Y 值作用的結(jié)果實例2、彩色目標(biāo)提取,(原理與色彩變換相同,應(yīng)用圖像像素的點的色度進(jìn)行判別,色度在附近時為紅色,附近為蘭色,附近為綠色)我們根據(jù)變換遠(yuǎn)離編輯出如下程序:clear;%清除工作區(qū)內(nèi)所有的變量close all;%關(guān)閉所有的figure%global i j y c1 c
10、2 sat hue;xsize=640;ysize=480;m_inty=1;m_hue=0;m_sat=2;y(1:xsize,1:ysize)=double(0.0);c1(1:xsize,1:ysize)=double(0.0);c2(1:xsize,1:ysize)=double(0.0);sat(1:xsize,1:ysize)=double(0.0);hue(1:xsize,1:ysize)=double(0.0);%path1= sprintf(red%d.bmp,k);Iinp=imread(c3.jpg);image_r(:,:)=Iinp(:,:,3);image_g(:,
11、:)=Iinp(:,:,2);image_b(:,:)=Iinp(:,:,1);%由RGB變成色差信號y,c1,c2=Rgb_to_yc(image_r,image_g,image_b,xsize,ysize);%由色差信號計算飽和度和色相sat,hue=C_to_SH(c1,c2,xsize,ysize);%亮度飽合度色調(diào)的調(diào)整%ss=max(sat(:);%x=0:1:360;%figure(10),hist(hue,x);%figure(11),histfit(hue,x)hi,hj,hall = find(hue20&hue20&hue20&hue 1-binocdf(100,162,
12、0.5)ans = 0.0010433相關(guān)函數(shù) HYPERLINK jar:file:/C:/Program%20Files/MATLAB/R2010a/help/toolbox/stats/help.jar%21/binofit.html binofit | HYPERLINK jar:file:/C:/Program%20Files/MATLAB/R2010a/help/toolbox/stats/help.jar%21/binoinv.html binoinv | HYPERLINK jar:file:/C:/Program%20Files/MATLAB/R2010a/help/tool
13、box/stats/help.jar%21/binopdf.html binopdf | HYPERLINK jar:file:/C:/Program%20Files/MATLAB/R2010a/help/toolbox/stats/help.jar%21/binornd.html binornd | HYPERLINK jar:file:/C:/Program%20Files/MATLAB/R2010a/help/toolbox/stats/help.jar%21/binostat.html binostat | HYPERLINK jar:file:/C:/Program%20Files/
14、MATLAB/R2010a/help/toolbox/stats/help.jar%21/cdf.html cdf附:二項式分布(binomial distribution )定義二項分布的概率密度函數(shù)為where k is the number of successes in n trials of a Bernoulli process with probability of success p.The binomial distribution is discrete, defined for integers k = 0, 1, 2, . n, where it is nonzero.
15、背景The binomial distribution models the total number of successes in repeated trials from an infinite population under the following conditions:Only two outcomes are possible on each of ntrials.The probability of success for each trial is constant.All trials are independent of each other.The binomial
16、 distribution is a generalization of the HYPERLINK jar:file:/C:/Program%20Files/MATLAB/R2010a/help/toolbox/stats/help.jar%21/brn2ivz-2.html Bernoulli distribution; it generalizes to the HYPERLINK jar:file:/C:/Program%20Files/MATLAB/R2010a/help/toolbox/stats/help.jar%21/brn2ivz-84.html multinomial di
17、stribution.參數(shù)Suppose you are collecting data from a widget manufacturing process, and you record the number of widgets within specification in each batch of100. You might be interested in the probability that an individual widget is within specification. Parameter estimation is the process of determ
18、ining the parameter, p, of the binomial distribution that fits this data best in some sense.One popular criterion of goodness is to maximize the likelihood function. The likelihood has the same form as the binomial pdf above. But for the pdf, the parameters (nandp) are known constants and the variab
19、le isx. The likelihood function reverses the roles of the variables. Here, the sample values (the xs) are already observed. So they are the fixed constants. The variables are the unknown parameters. MLE involves calculating the value of p that give the highest likelihood given the particular set of
20、data.The function HYPERLINK jar:file:/C:/Program%20Files/MATLAB/R2010a/help/toolbox/stats/help.jar%21/binofit.html binofit returns the MLEs and confidence intervals for the parameters of the binomial distribution. Here is an example using random numbers from the binomial distribution with n=100 and
21、p=0.9. r = binornd(100,0.9)r = 85 phat, pci = binofit(r,100)phat = 0.85pci = 0.76469 0.91355The MLE for parameterp is0.8800, compared to the true value of0.9. The 95% confidence interval forp goes from 0.7998 to0.9364, which includes the true value. In this made-up example you know the true value of
22、p. In experimentation you do not.示例The following commands generate a plot of the binomial pdf for n = 10 and p = 1/2.x = 0:10;y = binopdf(x,10,0.5);plot(x,y,+) 相關(guān)內(nèi)容 HYPERLINK jar:file:/C:/Program%20Files/MATLAB/R2010a/help/toolbox/stats/help.jar%21/bqt29ct.html l bqt290a-1 Discrete Distributions附:二項
23、式分布(網(wǎng)上)定義若某事件概率為p,現(xiàn)重復(fù)試驗n次,該事件發(fā)生k次的概率為:P=C(k,n)pk(1-p)(n-k)C(k,n)表示組合數(shù),即從n個事物中拿出k個的方法數(shù)。二項分布的概念考慮只有兩種可能結(jié)果的隨機(jī)試驗,當(dāng)成功的概率()是恒定的,且各次試驗相互獨立,這種試驗在統(tǒng)計學(xué)上稱為貝努里試驗(Bernoullitrial)。如果進(jìn)行n次貝努里試驗,取得成功次數(shù)為X(X=0,1,n)的概率可用下面的二項分布概率公式來描述:P=C(X,n) X(1-)(n-X)式中的n為獨立的貝努里試驗次數(shù),為成功的概率,(1-)為失敗的 HYPERLINK /doc/5399415.html t _blan
24、k 概率,X為在n次貝努里試驗中出現(xiàn)成功的次數(shù),C(X,n)表示在n次試驗中出現(xiàn)X的各種組合情況,在此稱為二項系數(shù)(binomialcoefficient)。內(nèi)容簡介二項分布,伯努里分布:進(jìn)行一系列試驗,如果1. 在每次試驗中只有兩種可能的結(jié)果,而且是互相對立的;2. 每次實驗是獨立的,與其它各次試驗結(jié)果無關(guān);3. 結(jié)果事件發(fā)生的 HYPERLINK /doc/5399415.html t _blank 概率在整個系列試驗中保持不變,則這一系列試驗稱為伯努力試驗。在這試驗中,事件發(fā)生的次數(shù)為一 HYPERLINK /doc/2273201.html t _blank 隨機(jī)事件,它服從二次分布。
25、二項分布可以用于可靠性試驗??煽啃栽囼灣3J峭度雗個相同的式樣進(jìn)行試驗T小時,而只允許k個式樣失敗,應(yīng)用二項分布可以得到通過試驗的概率。一個事件必然出現(xiàn),就說它100%要出現(xiàn)。100%=1,所以100%出現(xiàn)的含義就是出現(xiàn)的概率P=1。即必然事件的出現(xiàn)概率為1。若擲一枚硬幣,正面向上的結(jié)果的概率為0.5。反面向上的結(jié)果的概率也是0.5。則出現(xiàn)正面向上事件或者反面向上事件的概率就是0.5+0.5=1,即二者必居其一。若擲兩次硬幣,由獨立事件的概率乘法定理那么兩次都是正面(反面)向上的概率是0.50.5=0.25。另外第一個是正第二個是反的出現(xiàn)概率也是0.50.5=0.25。同理第一個反第二個正的出
26、現(xiàn)概率也是0.50.5=0.25。于是一正一反的概率是前面兩個情況的和,即0.25+0.25=20.25=0.5。它們的合計值仍然是1。binopdf二項分布的概率密度函數(shù)語法格式Y(jié) = binopdf(X,N,P)函數(shù)功能Y = binopdf(X,N,P) 計算X中每個X(i)的概率密度函數(shù),其中,N中對應(yīng)的N(i)為試驗數(shù),P中對應(yīng)的P(i)為每次試驗成功的概率。Y, N, 和 P 的大小類型相同,可以是向量、矩陣或多維數(shù)組。輸入的標(biāo)量將擴(kuò)展成一個數(shù)組,使其大小類型與其它輸入相一致。N中的值必須是正整數(shù),0 P 1 。對于給出的x和兩個參數(shù)n和p,二項分布概率密度函數(shù)為其中 q = 1
27、p。 y為n次獨立試驗中成功x次的概率,其中,每次試驗成功的概率為p。指示器函數(shù) I(0,1,.,n)(x) 確保x 取值為 0, 1, ., n。 示例一個質(zhì)量檢查技術(shù)員一天能測試200塊電路板。假設(shè)有 2% 的電路板有缺陷,該技術(shù)員在一天的測試中沒有發(fā)現(xiàn)有缺陷的電路板的概率是多少?binopdf(0,200,0.02)ans =0.0176質(zhì)量檢查技術(shù)員一天中最大可能檢測出有缺陷的電路板是多少塊?defects=0:200;y = binopdf(defects,200,.02);x,i=max(y);defects(i) ans =4相關(guān)函數(shù) HYPERLINK jar:file:/D:
28、/Program%20Files/MATLAB/help/toolbox/stats/help.jar%21/binocdf.html binocdf | HYPERLINK jar:file:/D:/Program%20Files/MATLAB/help/toolbox/stats/help.jar%21/binofit.html binofit | HYPERLINK jar:file:/D:/Program%20Files/MATLAB/help/toolbox/stats/help.jar%21/binoinv.html binoinv | HYPERLINK jar:file:/D:
29、/Program%20Files/MATLAB/help/toolbox/stats/help.jar%21/binornd.html binornd | HYPERLINK jar:file:/D:/Program%20Files/MATLAB/help/toolbox/stats/help.jar%21/binostat.html binostat | HYPERLINK jar:file:/D:/Program%20Files/MATLAB/help/toolbox/stats/help.jar%21/pdf.html pdfdlmread將以ASCII碼分隔的數(shù)值數(shù)據(jù)文件讀入到矩陣中語
30、法格式M = dlmread(filename)M = dlmread(filename, delimiter)M = dlmread(filename, delimiter, R, C)M = dlmread(filename, delimiter, range)函數(shù)功能M = dlmread(filename)讀取有分隔符的ASCII數(shù)值數(shù)據(jù)文件filename,并把數(shù)據(jù)給到矩陣M中。文件名(filename)以字符串形式用單引號括起來。 dlmread 從文件的格式中推斷分隔符。M = dlmread(filename, delimiter)指定分隔符delimiter。使用 t 代表制
31、表符 tab 作為分隔。M = dlmread(filename, delimiter, R, C)R和C指定了數(shù)據(jù)讀取數(shù)據(jù),其左上角的值位于文件中的第R行,第C列。R和C從0開始,所以R=0, C=0 指向文件中的第一個值。M = dlmread(filename, delimiter, range)讀取由range = R1 C1 R2 C2指定的區(qū)域塊, (R1,C1)是左上角,(R2,C2)是右下角。也可以使用電子表格表示法來指定,如range = A1.B7。備注All data in the input file must be numeric. dlmread does not
32、read files that contain nonnumeric data, even if the specified rows and columns contain only numeric data.若沒有指定分隔符,當(dāng)從文件格式中推斷分隔符時,連續(xù)的空格符當(dāng)作一個分隔符對待。若指定了分隔符,則重復(fù)的分隔符將分別作為單獨的分隔符對待。If you want to specify an R, C, or range input, but not a delimiter, set the delimiter argument to the empty string, (two cons
33、ecutive single quotes with no spaces in between, ). For example,M = dlmread(myfile.dat, , 5, 2)In this case, dlmread treats repeated white spaces as a single delimiter.Dlmread將用0填充沒有邊界的區(qū)域。有多行的數(shù)據(jù)文件,若以非空格分隔符結(jié)束,例如分號,則導(dǎo)入后會多產(chǎn)生全0的一列在最后。Dlmread在導(dǎo)入任何復(fù)數(shù)時,將其作為一個整體導(dǎo)入到一個復(fù)數(shù)單元中。下面是有效的復(fù)數(shù)格式:i|jExample: 5.7-3.1ii|jE
34、xample: -7j嵌入了空格的復(fù)數(shù)是不正確的格式,空格將被認(rèn)為是分隔符。示例例1Export a 5-by-8 test matrix M to a file, and read it with dlmread, first with no arguments other than the filename:M = gallery(integerdata, 100, 5 8, 0); dlmwrite(myfile.txt, M, delimiter, t)dlmread(myfile.txt)ans = 96 77 62 41 6 21 2 42 24 46 80 94 36 20 75
35、 85 61 2 93 92 82 61 45 53 49 83 74 42 1 28 94 21 90 45 18 90 14 20 47 68Now read a portion of the matrix by specifying the row and column of the upper left corner:dlmread(myfile.txt, t, 2, 3)ans = 92 82 61 45 53 42 1 28 94 21 90 14 20 47 68This time, read a different part of the matrix using a rang
36、e specifier:dlmread(myfile.txt, t, C1.G4)ans = 62 41 6 21 2 80 94 36 20 75 93 92 82 61 45 74 42 1 28 94例2Export matrix M to a file, and then append an additional matrix to the file that is offset one row below the first:M = magic(3);dlmwrite(myfile.txt, M*5 M/5, )dlmwrite(myfile.txt, M/3, -append, .
37、 roffset, 1, delimiter, )type myfile.txt40 5 30 1.6 0.2 1.215 25 35 0.6 1 1.420 45 10 0.8 1.8 0.4 2.6667 0.33333 21 1.6667 2.33331.3333 3 0.66667When dlmread imports these two matrices from the file, it pads the smaller matrix with zeros:dlmread(myfile.txt) 40.0000 5.0000 30.0000 1.6000 0.2000 1.200
38、0 15.0000 25.0000 35.0000 0.6000 1.0000 1.4000 20.0000 45.0000 10.0000 0.8000 1.8000 0.4000 2.6667 0.3333 2.0000 0 0 0 1.0000 1.6667 2.3333 0 0 0 1.3333 3.0000 0.6667 0 0 0替代作為dlmread的替代,可使用導(dǎo)入向?qū)А_x擇菜單 “File | Import Data” 激活導(dǎo)入向?qū)?。相關(guān)函數(shù) HYPERLINK jar:file:/D:/Program%20Files/MATLAB/help/techdoc/help.jar
39、%21/ref/dlmwrite.html dlmwrite | HYPERLINK jar:file:/D:/Program%20Files/MATLAB/help/techdoc/help.jar%21/ref/textscan.html textscanfprintf寫數(shù)據(jù)到文本文件或輸出到命令窗口語法格式fprintf(fileID, format, A, .)fprintf(format, A, .)count = fprintf(.)函數(shù)功能fprintf(fileID, format, A, .) fileID為文件句柄,指定要寫入的文件;format是用來控制所寫數(shù)據(jù)格式的格式
40、符;A是用來存放數(shù)據(jù)的矩陣。applies the format to all elements of array A and any additional array arguments in column order, and writes the data to a text file. fprintf uses the encoding scheme specified in the call to HYPERLINK jar:file:/D:/Program%20Files/MATLAB/help/techdoc/help.jar%21/ref/fopen.html fopen.fp
41、rintf(format, A, .) 將A中的數(shù)據(jù)按格式format輸出到命令窗口。count = fprintf(.) 返回fprintf寫的字節(jié)數(shù)。輸入?yún)?shù)fileID取值為下面中的一種:從文件打開時所得到的文件句柄,為一個整數(shù)值。1 為標(biāo)準(zhǔn)輸出設(shè)備(屏幕)。2 for standard error.fileID缺省時,輸出到屏幕,即1為默認(rèn)值。format單引號括起來的是 “轉(zhuǎn)換控制字符串 ”,指定輸出的格式。由下面給出的各部分的組合而成:百分號后緊跟的格式字符,如%s 。輸出區(qū)域的寬度,精度,和其它選項(附加格式說明符)。要原樣輸出的文本(普通字符)。轉(zhuǎn)義字符,包括:單引號% 百分號
42、反斜線a響鈴b退格符fForm feedn換行r回車t水平制表符v垂直制表符xN十六進(jìn)制數(shù), NN八進(jìn)制數(shù), N轉(zhuǎn)換字符和可選的操作符按以下順序排列(包括空格):下表列出可用的轉(zhuǎn)換字符和限定類型字符。數(shù)據(jù)類型轉(zhuǎn)換說 明Integer, signed%d 或 %i十進(jìn)制整數(shù)%ld 或 %li64-bit base 10 values%hd 或 %hi16-bit base 10 valuesInteger, unsigned%u十進(jìn)制整數(shù)%o八進(jìn)制整數(shù)(octal)%x十六進(jìn)制整數(shù) (hexadecimal), 使用字母 af%X十六進(jìn)制整數(shù) (hexadecimal), 使用字母AF%lu%l
43、o%lx or %lX64-bit values, 十、八、十六進(jìn)制整數(shù)%hu%ho%hx or %hX16-bit values, 十、八、十六進(jìn)制整數(shù)Floating-point number%f定點表示法%e指數(shù)表示法,如 3.141593e+00%E同 %e,但用E,如 3.141593E+00%g%e 或 %f的緊湊格式,沒有尾部的0%G%E 或 %f的緊湊格式,沒有尾部的0%bx or %bX%bo%bu雙精度的十六、八、十進(jìn)制數(shù)值如:%bx prints pi as 400921fb54442d18%tx or %tX%to%tu單精度的十六、八、十進(jìn)制數(shù)值如:%tx print
44、s pi as 40490fdbCharacters%c單個字符%s字符串附加格式說明符,包括:字段寬度Minimum number of characters to print. Can be a number, or an asterisk (*) to refer to an argument in the input list. For example, the input list (%12d, intmax) is equivalent to (%*d, 12, intmax).精度For %f, %e, or %E: Number of digits to the right o
45、f the decimal point.Example: %6.4f prints pi as 3.1416For %g or %G Number of significant digits.Example: %6.4g prints pi as 3.142Can be a number, or an asterisk (*) to refer to an argument in the input list. For example, the input list (%6.4f, pi) is equivalent to (%*.*f, 6, 4, pi).FlagsActionFlagEx
46、ampleLeft-justify.%-5.2fPrint sign character (+ or ).+%+5.2fInsert a space before the value. % 5.2fPad with zeros.0%05.2fModify selected numeric conversions:For %o, %x, or %X, print 0, 0 x, or 0X prefix.For %f, %e, or %E, print decimal point even when precision is 0.For %g or %G, do not remove trail
47、ing zeros or decimal point.#%#5.0fIdentifierOrder for processing inputs. Use the syntax n$, where n represents the position of the value in the input list.For example, %3$s %2$s %1$s %2$s prints inputs A, B, C as follows: C B A B.The following limitations apply to conversions:Numeric conversions pri
48、nt only the real component of complex numbers.If you apply an integer or string conversion to a numeric value that contains a fraction, MATLAB overrides the specified conversion, and uses %e.If you apply a string conversion (%s) to integer values, MATLAB converts values that correspond to valid char
49、acter codes to characters. For example, %s converts 65 66 67 to ABC.Different platforms display exponential notation (such as %e) with a different number of digits in the exponent.環(huán)境示例Windows1.23e+004UNIX1.23e+04不同的操作系統(tǒng)環(huán)境顯示的負(fù)零(-0) 不相同。轉(zhuǎn)換字符環(huán)境%e 或 %E%f%g 或 %GWindows0.000000e+0000.0000000Others-0.00000
50、0e+00-0.000000-0A數(shù)值或字符數(shù)組。示例例1 輸出多個數(shù)值和文本到屏幕。 B = 8.8 7.7 ; 8800 7700; fprintf(X is %4.2f meters or %8.3f mmn, 9.9, 9900, B)X is 9.90 meters or 9900.000 mmX is 8.80 meters or 8800.000 mmX is 7.70 meters or 7700.000 mm注:對B是按列處理。例2 對雙精度數(shù)四舍五入取整,并輸出到屏幕。 a = 1.02 3.04 5.06; fprintf(%dn, round(a);135例3 寫一組數(shù)
51、據(jù)到文本文件中。x = 0:.1:1;y = x; exp(x); % 打開文件exp.txt為可寫fid = fopen(exp.txt, w);fprintf(fid, %6.2f %12.8fn, y);fclose(fid); % 查看文件內(nèi)容type exp.txtMATLAB導(dǎo)入功能,所有UNIX應(yīng)用程序,和微軟Word和寫字板都把 n 作為一個換行符指示器。然而,如果你要用微軟記事本讀取文件,請使用“ r n”移動到新行再寫。將之前的調(diào)用改寫如下:fprintf(fid, %6.2f %12.8frn, y);輸出結(jié)果不變。例4 在Windows系統(tǒng)上,將PC形式的指數(shù)表示法(三
52、位數(shù)的指數(shù))轉(zhuǎn)換到unix形式的表示法(兩位數(shù)字),并打印數(shù)據(jù)到一個文件:a = 0.06 0.1 5 300 % 用sprintf轉(zhuǎn)換數(shù)值數(shù)據(jù)到文本,使用%ea_str = sprintf(%et,a) % use strrep to replace exponent prefix with shorter versiona_str = strrep(a_str,e+0,e+);a_str = strrep(a_str,e-0,e-); %調(diào)用fprintf打印更新的文本字符串fid = fopen(newfile.txt,w);fprintf(fid, %s, a_str);fclose(
53、fid); % 查看文件內(nèi)容type newfile.txt例5 在屏幕上顯示一個超鏈接。例6 創(chuàng)建一個字符矩陣并存入磁盤,再讀出賦值給另一個矩陣。 a=string; fid=fopen(d:char1.txt,w); fprintf(fid,%s,a); fclose(fid); fid1=fopen(d:char1.txt,rt); b=fscanf(fid1,%s)b =string例7 用fprintf輸出一個數(shù)值矩陣。i = 1:10;square_root = sqrt(i);square = i.2;cube = i.3;% 拼接成一個大矩陣,行向量轉(zhuǎn)置成列向量out = i square_root square cube ;% 輸出數(shù)據(jù)for i = 1:10fprintf ( %2d %11.4f %6d %8dn,out(i,:);end%第1列整數(shù)輸出,占2格%第2列實數(shù)輸出,占11格,4位小數(shù)i = 1:10;square_root = sqrt(i);square = i.2;cube = i.3;% 拼接成一個大矩陣out = i; square_root; square; cube;% 輸出數(shù)據(jù)fprintf ( %2d %11.4f %6d %8dn,out);%將第1列輸出為第1行,格式對應(yīng);%將
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年肇東特崗面試題及答案
- 個人入暗股合同范例
- 公司聘請家政合同范例
- 儲罐保溫施工合同范例
- 丙烷管道施工合同范例
- 借款合同附帶租賃合同范例
- 中考化學(xué)二輪復(fù)習(xí) 計算題特訓(xùn)題型6 技巧型計算(含解析)
- 集體產(chǎn)權(quán)房屋買賣合同范本
- 二手印刷機(jī)轉(zhuǎn)讓合同范本
- 合作社轉(zhuǎn)讓協(xié)議書
- 密封條范文模板(A4打印版)
- 醫(yī)療機(jī)構(gòu)制劑管理規(guī)范
- JBT 11699-2013 高處作業(yè)吊籃安裝、拆卸、使用技術(shù)規(guī)程
- 2023年 新版評審準(zhǔn)則質(zhì)量記錄手冊表格匯編
- 2024年全國版圖知識競賽(小學(xué)組)考試題庫大全(含答案)
- 博物館保安服務(wù)投標(biāo)方案(技術(shù)方案)
- (高清版)TDT 1047-2016 土地整治重大項目實施方案編制規(guī)程
- 2024年新疆維吾爾自治區(qū)中考一模綜合道德與法治試題
- 醫(yī)藥代表專業(yè)化拜訪技巧培訓(xùn)
- 今年夏天二部合唱譜
- 小米公司招聘測試題目
評論
0/150
提交評論