matlab中disp函數(shù)的使用 | 您所在的位置:網(wǎng)站首頁(yè) › 屬羊54歲是哪一年的出生 › matlab中disp函數(shù)的使用 |
? ? disp函數(shù)直接將內(nèi)容輸出在Matlab命令窗口中, ?? 關(guān)鍵是看disp函數(shù)怎么把字符和數(shù)字在一起進(jìn)行顯示。 matlab中disp()就是屏幕輸出函數(shù),類似于c語(yǔ)言中的printf()函數(shù) ? %%以下是一個(gè)通過給定兩點(diǎn)顯示直線方程的程序, %%該程序需要給出兩個(gè)點(diǎn)的坐標(biāo),結(jié)果返回為y=kx+b的格式,且求得斜率 function [k,a1,b,type]=straight_line(A,B) % 輸入,A,B兩點(diǎn)坐標(biāo) V=B-A; a=inf; b=inf; type='undefined'; if A==B 'The two points are the same' return end if V(1)==0 && V(2)==0 disp('Enter two distinct points next time') return end if V(1)==0 type='vertical'; elseif V(2)==0 type='horizontal'; else type='oblique'; slope=atan2(V(2),V(1)); s=inv([A(1) 1;B(1) 1])*[A(2) B(2)]'; a=s(1); b=s(2); end switch type case 'vertical' disp('經(jīng)過這兩個(gè)點(diǎn)的直線方程為::'); disp(['x = ',num2str(A(1))]); case 'horizontal' disp(' 經(jīng)過這兩個(gè)點(diǎn)的直線方程為:: '); disp(['y =',num2str(A(2))]) ; case 'oblique' disp(' 經(jīng)過這兩個(gè)點(diǎn)的直線方程為:') ; disp(['y = ',num2str(a) ,' *x +',num2str(b)]); disp('斜率為:') k=num2str(a);%將符號(hào)數(shù)值化 end? disp(X)函數(shù)只有一個(gè)輸入,當(dāng)你有多個(gè)字符串作為輸入時(shí)就會(huì)報(bào)錯(cuò)。 例如: disp('Alice is ' , num2str(12) , ' years old!' ); 就會(huì)報(bào)錯(cuò)--輸入?yún)?shù)過多。 但是將里邊的內(nèi)容用中括號(hào)一括就成了一個(gè)字符串, 例如: str=['Alice is ' num2str(12) ' years old!']; disp(str); 上邊這句話也就等價(jià)于: disp=(['Alice is ' num2str(12) ' years old!']); 這就是加中括號(hào)的原因,而不是因?yàn)閚um2str(), 因?yàn)閐isp(num2str(12));也是正確的,因?yàn)槔镞吘椭挥幸粋€(gè)字符串。 ? |
CopyRight 2018-2019 實(shí)驗(yàn)室設(shè)備網(wǎng) 版權(quán)所有 |