過冷水今天和大家分享一下讀取圖像數據點的小技巧:用cftool插值繪圖得到擬合後的圖像,然後正確獲取擬合圖像對應的數據。
有時候的實驗室數據圖像得到的如下圖所示:
當然之前中過冷水多次有跟大家提多項式擬合、傅立葉級數擬合、高斯級數擬合,實際更加常見的操作是用matlab中圖像擬合工具箱cftool靈活進行函數擬合。
這個過程大多數讀者應該都有了解,問題在於:「how to get fit data」?首先我們需要先把函數工具擬合方法生成代形式:
function [fitresult, gof] = createFit(x1, y1)%CREATEFIT(X1,Y1)% Create a fit.%% Data for 'untitled fit 1' fit:% X Input : x1% Y Output: y1% Output:% fitresult : a fit object representing the fit.% gof : structure with goodness-of fit info.%% 另請參閱 FIT, CFIT, SFIT.
% 由 MATLAB 於 06-Sep-2020 19:30:43 自動生成
%% Fit: 'untitled fit 1'.[xData, yData] = prepareCurveData( x1, y1 );
% Set up fittype and options.ft = fittype( 'smoothingspline' );
% Fit model to data.[fitresult, gof] = fit( xData, yData, ft );
% Plot fit with data.figure( 'Name', 'untitled fit 1' );h = plot( fitresult, xData, yData );legend( h, 'y1 vs. x1', 'untitled fit 1', 'Location', 'NorthEast' );% Label axesxlabel x1ylabel y1grid on生成代碼形式後,對代碼進行簡化,可以得到如下代碼:
clear;x1=[2,2.89795918367347,2.94897959183673,2.97959183673469,2.98979591836735,3.01020408163265,3.03061224489796,3.06122448979592,3.10204081632653,3.14285714285714,3.24489795918367,3.29591836734694,3.34693877551020,3.39795918367347,3.43877551020408,3.48979591836735,3.56122448979592,3.67346938775510,3.82653061224490,4.06122448979592,4.32653061224490,4.65306122448980,5,5.26530612244898,5.56122448979592,5.88775510204082,6.11224489795918,6.33673469387755,6.62244897959184,6.98979591836735,7.47959183673469,7.98979591836735];y1=[0,0.524822695035461,0.978723404255319,1.31205673758865,1.57446808510638,1.82978723404255,2.04964539007092,2.31914893617021,2.63120567375887,2.85815602836879,2.82269503546099,2.59574468085106,2.34042553191489,2.08510638297872,1.87234042553192,1.64539007092199,1.38297872340426,1.11347517730496,0.936170212765958,0.858156028368794,0.900709219858156,0.964539007092199,0.929078014184397,0.829787234042553,0.787234042553191,0.872340425531915,1.02836879432624,1.17730496453901,1.20567375886525,1.07801418439716,0.964539007092199,0.921985815602837];f=fit(x1',y1','smoothingspline');x2=linspace(2,8,100)';y2=f(x2);figure1 = figure;subplot1 = subplot(2,1,1,'Parent',figure1);hold(subplot1,'on');plot(x1,y1,'Parent',subplot1,'LineWidth',2);xlabel('$x$','Interpreter','latex');ylabel('$y$','Interpreter','latex');title('原始數據','Interpreter','latex');set(subplot1,'FontSize',12,'LineWidth',2);subplot2 = subplot(2,1,2,'Parent',figure1);hold(subplot2,'on');plot(x2,y2,'Parent',subplot2,'LineWidth',2);xlabel('$x$','Interpreter','latex');ylabel('$y$','Interpreter','latex');title('插值擬合數據','Interpreter','latex');set(subplot2,'FontSize',12,'LineWidth',2);這樣(x2,y2)就是我們想要的所有插值擬合數據,還有一種查繪圖數據的方法。
h=plot(x1,y1)x=get(h,'xdata');y=get(h,'ydata');該段代碼主要是有時候再特殊情況下我們先是得到具體的函數圖像而不是繪圖數據,所以就需要使用該段代碼就有用了。冷水現在再講講如何提取特殊點方法。可以用
[X,Y]=ginput(2)X = 3.2373 3.4171Y = 2.7808 2.2558、
本期過冷水和大家分享的知識容量短小精煉,對於初學者都能夠使用上而不是以往的那些晦澀深奧的知識,希望能夠對讀者有幫助。
往期回顧>>>>>>matlab動態繪圖
matlab繪圖(五)
520!公眾號抖音【跳動的心】原始碼分享
基於奇怪的羊返航,聊plot圖像鏡像
200多款plot/plot3自定義marker任你選
在matlab愛好者公眾號中,回復「QQ」加入公眾號專屬Q群;回復「原創」獲取小編原創代碼;回復「星球」加入資源分享園地知識星球。
如需轉載,請在公眾號中回復「轉載」獲取授權,未經授權擅自搬運抄襲的,必將追究其責任!