Matlab uicontrol举例

这个例子显示一个理想采样信号及其幅度谱和相位谱,并可以修改参数。 此外还包括几个其他的信号。

主文件spy102.m

%初始化根窗体 clf reset; set(gcf,'Units','pixels','position' ,[185 50 860 655],'name', '信号谱线分析',... 'numbertitle', 'off', 'Tag', 'dsp'); set(gcf, 'defaultuicontrolfontsize' ,12); set(gcf, 'defaultuicontrolfontname' , ' 宋体 ' ); %添加组件 %三个axes,用来显示图像 labelTitle1 = uicontrol(gcf,'Style', 'text', 'String', '理想采样信号序列',... 'Position',[60 610 660 20], 'BackgroundColor', [.8 .8 .8]); axesH1 = axes('Units','pixels','Position', [50 450 660 160]); labelTitle2 = uicontrol(gcf,'Style', 'text', 'String', '理想采样信号的幅度谱',... 'Position', [60 405 660 20], 'BackgroundColor', [.8 .8 .8]); axesH2 = axes('Units','pixels','Position', [50 240 660 160]); labelTitle3 = uicontrol(gcf,'Style', 'text', 'String', '理想采样信号的相位谱',... 'Position', [60 195 660 20], 'BackgroundColor', [.8 .8 .8]); axesH3 = axes('Units','pixels','Position', [50 30 660 160]); %右侧编辑区域,包含几个用于画图的参数A、α、ω、T labelTT = uicontrol(gcf,'Style', 'text', 'String', '理想采样信号','Position', [750 575 80 30],... 'BackgroundColor', [.8 .8 .8], 'FontSize', 10); labelA = uicontrol(gcf,'Style', 'text', 'String', 'A','Position', [760 545 60 20],... 'BackgroundColor', [.8 .8 .8], 'FontSize', 10); dataA = uicontrol(gcf,'Style', 'edit', 'String', '1','Position', [760 525 60 20], 'FontSize', 10); labelAlpha = uicontrol('Style', 'text', 'String', 'α','Position', [760 470 60 20],... 'BackgroundColor', [.8 .8 .8], 'FontSize', 10); dataAlpha = uicontrol(gcf,'Style', 'edit', 'String', '0.4','Position', [760 450 60 20], 'FontSize', 10); labelW = uicontrol(gcf,'Style', 'text', 'String', 'ω','Position', [760 395 60 20],... 'BackgroundColor', [.8 .8 .8], 'FontSize', 10); dataW = uicontrol(gcf,'Style', 'edit', 'String', '2.0734','Position', [760 375 60 20], 'FontSize', 10); labelT = uicontrol(gcf,'Style', 'text', 'String', 'T','Position', [760 320 60 20],... 'BackgroundColor', [.8 .8 .8], 'FontSize', 10); dataT = uicontrol(gcf,'Style', 'edit', 'String', '1','Position', [760 300 60 20], 'FontSize', 10); %用于更改图像的按钮 btnH = uicontrol(gcf,'Style', 'pushbutton', 'String', 'Plot',... 'Position', [755 240 70 25], 'FontSize', 10); %列表框,用于显示其他几种图形 labelOt = uicontrol(gcf,'Style', 'text', 'String', '其他','Position', [743 180 100 20],... 'BackgroundColor', [.8 .8 .8], 'FontSize', 10); listboxH = uicontrol(gcf,'Style', 'listbox', 'String', {'单位脉冲序列';'矩形序列';'特定冲击串';'卷机计算';'卷积定律验证'},... 'Position', [743 80 100 80], 'FontSize', 10); %设置回叫函数和初始化图像显示 set(btnH,'Callback', 'btnCall(axesH1,axesH2,axesH3,dataA,dataAlpha,dataW,dataT,labelTitle1,labelTitle2,labelTitle3)'); set(listboxH,'Callback', 'listboxCall(listboxH,axesH1,axesH2,axesH3,labelTitle1,labelTitle2,labelTitle3)'); btnCall(axesH1,axesH2,axesH3,dataA,dataAlpha,dataW,dataT,labelTitle1,labelTitle2,labelTitle3);

btnCall.m

function varargout = btnCall(axesH1,axesH2,axesH3,dataA,dataAlpha,dataW,dataT,labelTitle1,labelTitle2,labelTitle3) %BTNH_CALLBACK Summary of this function goes here % Detailed explanation goes here A = str2double(get(dataA,'String')); a = str2double(get(dataAlpha,'String')); w0 = str2double(get(dataW,'String')); T = str2double(get(dataT,'String')); %信号曲线 n=0:50; x=A*exp(-a*n*T).*sin(w0*n*T); stem(axesH1,n,x); set(axesH1,'XGrid','on','YGrid','on'); set(labelTitle1,'String','理想采样信号序列'); %幅度谱 k=-25:25; W=(pi/12.5)*k; X=x*(exp(-j*pi/12.5)).^(n'*k); magX=abs(X); stem(axesH2,magX); set(axesH2,'XGrid','on','YGrid','on'); set(labelTitle2,'String','理想采样信号的幅度谱'); %相位谱 angX=angle(X); stem(axesH3,angX); set(axesH3,'XGrid','on','YGrid','on'); set(labelTitle3,'String','理想采样信号的相位谱'); 

white-space: pre-wrap; background-color: #ffffff;">listboxCall.m

function varargout = listboxCall(listboxH,axesH1,axesH2,axesH3,labelTitle1,labelTitle2,labelTitle3) value = get(listboxH,'Value'); %判断选中的值,显示相应的图形 if value==1 uis(axesH1,axesH2,axesH3,labelTitle1,labelTitle2,labelTitle3); elseif value==2 recs(axesH1,axesH2,axesH3,labelTitle1,labelTitle2,labelTitle3); elseif value==3 maic(axesH1,axesH2,axesH3,labelTitle1,labelTitle2,labelTitle3); elseif value==4 convo(axesH1,axesH2,axesH3,labelTitle1,labelTitle2,labelTitle3); elseif value==5 conlc(axesH1,axesH2,axesH3,labelTitle1,labelTitle2,labelTitle3); end %单位脉冲序列 function uis(axesH1,axesH2,axesH3,labelTitle1,labelTitle2,labelTitle3) n=1:50; x=zeros(1,50); x(1)=1; %信号曲线 stem(axesH1,n,x); set(axesH1,'XGrid','on','YGrid','on'); set(labelTitle1,'String','单位脉冲序列'); %幅度谱 k=-25:25; X=x*(exp(-j*pi/12.5)).^(n'*k); magX=abs(X); stem(axesH2,magX); set(axesH2,'XGrid','on','YGrid','on'); set(labelTitle2,'String','单位冲击信号的幅度谱'); %相位谱 angX=angle(X); stem(axesH3,angX); set(axesH3,'XGrid','on','YGrid','on'); set(labelTitle3,'String','单位冲击信号的相位谱'); %矩形序列 function recs(axesH1,axesH2,axesH3,labelTitle1,labelTitle2,labelTitle3) n=1:5; x=sign(sign(10-n)+1); %信号曲线 stem(axesH1,n,x); set(axesH1,'XGrid','on','YGrid','on'); set(labelTitle1,'String','矩形序列'); %幅度谱 k=-25:25; X=x*(exp(-j*pi/25)).^(n'*k); magX=abs(X); stem(axesH2,magX); set(axesH2,'XGrid','on','YGrid','on'); set(labelTitle2,'String','矩形序列的幅度谱'); %相位谱 angX=angle(X); stem(axesH3,angX); set(axesH3,'XGrid','on','YGrid','on'); set(labelTitle3,'String','矩形序列的相位谱'); %特定冲击串序列 function maic(axesH1,axesH2,axesH3,labelTitle1,labelTitle2,labelTitle3) n=1:50; % 义序列的长度是50 x=zeros(1,50); %注意:MATLAB 中数组下标从1 始 x(1)=1; x(2)=2.5; x(3)=2.5; x(4)=1; %信号曲线 stem(axesH1,n,x); set(axesH1,'XGrid','on','YGrid','on'); set(labelTitle1,'String','特定冲击串序列n(x)=δ(n)+2.5δ(n-1)+2.5δ(n-2)+δ(n-3)'); %特定冲击串序列的幅度谱 k=-25:25; X=x*(exp(-j*pi/12.5)).^(n'*k); magX=abs(X); %绘制x(n) 的幅度谱 stem(axesH2,magX); set(axesH2,'XGrid','on','YGrid','on'); set(labelTitle2,'String','特定冲击串序列的幅度谱'); %特定冲击串序列的相位谱 angX=angle(X); %绘制x(n) 的相位谱 stem(axesH3,angX); set(axesH3,'XGrid','on','YGrid','on'); set(labelTitle3,'String','特定冲击串序列的相位谱'); %卷机计算 function convo(axesH1,axesH2,axesH3,labelTitle1,labelTitle2,labelTitle3) n=1:50; % 义序列的长度是50 hb=zeros(1,50); %注意:MATLAB 中数组下标从1 始 hb(1)=1; hb(2)=2.5; hb(3)=2.5; hb(4)=1; %系统h[n] stem(axesH1,hb); set(axesH1,'XGrid','on','YGrid','on'); set(labelTitle1,'String','系统hb[n]'); %输入信号x[n] m=1:50; T=0.001; % 义序列的长度是和采样率 A=444.128; a=50*sqrt(2.0)*pi; %设置信号有关的参数 w0=50*sqrt(2.0)*pi; x=A*exp(-a*m*T).*sin(w0*m*T); %pi 是MATLAB 义的π,信号乘可采用“.*” stem(axesH2,magX); set(axesH2,'XGrid','on','YGrid','on'); set(labelTitle2,'String','输入信号x[n]'); %输出信号y[n] y=conv(x,hb); stem(axesH3,angX); set(axesH3,'XGrid','on','YGrid','on'); set(labelTitle3,'String','输出信号y[n]'); %卷积定律验证 function conlc(axesH1,axesH2,axesH3,labelTitle1,labelTitle2,labelTitle3) figure(2); n=1:50; % 义序列的长度是50 hb=zeros(1,50); %注意:MATLAB 中数组下标从1 始 hb(1)=1; hb(2)=2.5; hb(3)=2.5; hb(4)=1; subplot(3,1,1);stem(hb); title( '系统hb[n]'); m=1:50; T=0.001; % 义序列的长度是和采样率 A=444.128; a=50*sqrt(2.0)*pi; %设置信号有关的参数 w0=50*sqrt(2.0)*pi; x=A*exp(-a*m*T).*sin(w0*m*T); k=-25:25; X=x*(exp(-j*pi/12.5)).^(n'*k); magX=abs(X); %绘制x(n) 的幅度谱 subplot(3,2,1);stem(magX);title('输入信号的幅度谱'); angX=angle(X); %绘制x(n) 的相位谱 subplot(3,2,2);stem(angX) ; title ('输入信号的相位谱') Hb=hb*(exp(-j*pi/12.5)).^(n'*k); magHb=abs(Hb); %绘制hb(n) 的幅度谱 subplot(3,2,3);stem(magHb);title( '系统响应的幅度谱'); angHb=angle(Hb); %绘制hb(n) 的相位谱 subplot(3,2,4);stem(angHb) ; title ( '系统响应的相位谱') n=1:99; k=1:99; y=conv(x,hb); Y=y*(exp(-j*pi/12.5)).^(n'*k); magY=abs(Y); %绘制y(n) 的幅度谱 subplot(3,2,5);stem(magY);title('输出信号的幅度谱'); angY=angle(Y); %绘制y(n) 的相位谱 subplot(3,2,6);stem(angY) ; title ('输出信号的相位谱') %以下将验证的结果显示 figure(3); XHb=X.*Hb; subplot(2,1,1);stem(abs(XHb));title( 'x(n) 的幅度谱与hb(n)幅度谱相乘'); subplot(2,1,2);stem(abs(Y));title( 'y(n) 的幅度谱'); axis([0,60,0,8000])

你可能感兴趣的:(String,function,matlab,callback,图形,plot)