




已阅读5页,还剩6页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
matlab课程设计设计名称:基于matlab的fft-信号分析仪的实现研 究 生: 李 文 哲 指导教师: 李 彩 虹 学科专业: 计算机技术 完成时间: 2013年1月14日 matlab 是一套高性能的数值计算和可视化的科技应用软件。它集高效的数值分析、完备的信号和图形处理、功能丰富的应用工具箱为一体,构成了一个方便快捷,界面友好的用户环境,是一种适应多种硬件平台的数学计算工具,它的出现给各课程的计算机辅助教学带来了福音。特别是他的集成图形用户界面guide(graphical user interface development environment),包含了窗口菜单、对话框、按钮和文本等各种控件的用户界面,用户通过键盘或鼠标操作,就可以设计出具有自己独特风格的图形界面,再通过编写回调函数就可以实现gui 与用户之间的交互,为教学课件的制作提供了极大的方便,guide 程序设计分两步进行,一是静态图形界面制作,二是控件回调函数编程。1 静态图形界面制作在matlab 的命令窗口中键入“guide”命令,启动guide 的gui 编辑器,gui 控制面板包括了所有的图形界面控件uicontrol,如按钮(push butter)、滑动条(slider)、单选按钮(radio butter)、复选框(check box)、文本框(edit text)、文本标签(ststic text)、下拉菜单(popup menu)、下拉列表框(list box)、双位按钮(toggle butter)、坐标轴(axes)等,用户选中需要的控件,拖移到空白处,即可创建出相应的控件,并通过拖拉可调整其大小大小,如图1 所示。图1每个控件都有自己的属性style,属性style 决定控件的类型,将鼠标移至相应的控件上双击,就可以打开属性框,每个控件均有很多项属性,但主要的有:“string” 用来定义控件的显示字符;“tag”是控件的重要属性,是对控件进行编程和访问时的标识名称,每个控件创建时都会由开发环境自动生成一个标识,在程序设计中为了编辑、记忆和维护的方便,用户要修改成自己好记忆的字符串,在编写回调函数callback 时由此标识来识别;“name”是标题属性;“backgroundcolor”是背景颜色设置,按用户风格设定其值。图1 中各控件的“string”属性和“tag”属性分别定义如下:三个坐标的“tag”标识分别为:axes10、axes1、axes2;两个按钮的“tag”标识分别为:按钮“冲激响应”的“tag”标识为pushbutton1,按钮“阶跃响应” 的“tag”标识为pushbutton2;编辑文本框的“tag”标识为rlc,用来接收键盘输入电阻、电感及电容的值。按照自己的构想将需要的控件位置摆放合适,属性设置好后保存gui 文件,文件名取为test,matlab 将用户创建的gui 的所用信息保存在test.fig 和test.m 两个文件中,其中.fig 为二进制文件,用来存放搭建gui 界面所需的所有图形控件的完整描述信息(包括控件属性);.m 文件用来存放gui 程序响应特定事件时调用的函数,包含了运行gui 所需的所有程序代码,包括gui 控件的回调函数callback 框架,.m 决定gui 的行为,他控制gui 中控件如何响应用户操作,这两个文件是互相影响的。系统自动生成的test.m 文件代码中有三个关键函数需要用户编写程,分别说明如下:openingfcn 函数openingfcn 函数是guide 在.m 文件中自动生成的函数之一,其名字与文件名相同,如文件名为test,则openingfcn 函数的函数名为test_openingfcn,openingfcn 中的代码在运行gui 并使其可视化之前执行,因此该函数通常用来实现用户访问gui 之前所需的各种数据的初始化。createfcn 函数guide 在为gui 自动生成.m 文件中,自动为每一个控件生成createfcn 函数,函数名由控件的“tag”属性决定,主要用来存放相应控件的初始化函数。callback 函数guide 在为gui 自动生成.m 文件中,为每一个控件生成相应的回调函数callback 定义行代码,控件对用户的响应控制,需要在回调函数中添加相应的代码来实现。控件回调函数的函数名由控件的“tag”属性决定。回调函数的编写是guide 设计的重点,所有句柄对象都有如下三个回调函数:buttondownfcn(),鼠标左键在对象上单击时执行的回调函数;createfcn(),对象创建时执行的回调函数;deletefcn(),对象删除时执行的回调函数。回调都是可以在命令窗口中可执行的字符串,当属性激活时,它传给eval 函数来执行。回调编程可以采用不同的形式或不同的调用方式来执行相应的程序。在编写回调函数时句柄图形函数,常用的几个句柄函数为:set 设置对象属性;get 获取对象属性;gcf 获得当前图形的句柄;gca 获得当前坐标轴的句柄;gco 获得当前对象的句柄;axes 创建坐标轴对象;image 创建图形对象等。2 编程控制gui在静态图形界面设计好后,用户可以通过鼠标或键盘激活这些图形对象,使计算机执行相应的动作,这些动作的执行通过回调函数callback 来实现,matlab guide 编程主要考虑的是界面函数如何通过回调函数来实现,以及用什么样的方法比较方便快捷。表1 中test.m 代码中只有各个控件、菜单的回调函数原型和注释,并没有实现功能的函数体,要实现具体的功能调用需要自己编写添加程序。2.1 初始化程序的编写初始化图形界面是通过函数openingfig 实现的。openingfig 函数调用与.m 文件对应的.fig 文件来初始化图形界面。在这一过程中还存在隐含调用.fig 的creatfcn 函数,但这一过程无法使用输入参数,也就是说要用输入参数设置图形界面元素的一些特性,还必须编写自己的初始化函数。在本设计实例中,需要在坐标轴ases10 的位置显示电路图,需要为“tag”标识为rlc 的编辑文本框设置r、l、c 初始值,其初始化程序分别为:function test_openingfcn(hobject, eventdata, handles, varargin)i=imread(dl.bmp,bmp); % 读入图形文件dl.bmpaxes(handles.axes10); % 获得坐标轴axes10 的句柄image(i);axis off % 显示电路图function rlc_createfcn(hobject, eventdata, handles)set(gcbo,string,1 1 1); % 为r、l、c 设初始值if ispc & isequal(get(hobject,backgroundcolor), get(0,defaultuicontrolbackgroundcolor)set(hobject,backgroundcolor,white);end2.2 回调函数callback 的编写回调函数的编写,可以把该控件的函数代码直接写在“callback”中,也可以把函数代码放在一个自定义的.m 文件中,而在callback”中只将其文件名写上即可。不管采用哪种方法,回调函数编写的一个重要的技巧就是通过set、get 语句对控件的属性进行获取、设置,以及完成相关数据的传递、保存、获取,这一点在下面的程序段中得到了很好的体现。在本设计实例中,当按下“冲激响应”按钮时,要在坐标轴ases1 的位置显示冲激响应曲线,当按下“阶跃响应”按钮时,要在坐标轴ases2 的位置显示阶跃响应曲线,所以要分别为pushbutton1_callback 和pushbutton2_callback 编写回调函数,程序代码分别为:function pushbutton1_callback(hobject, eventdata, handles)m= str2num(get(handles.rlc,string)r=m(1);l=m(2);c=m(3);a=l*c r*c 1;b=1;axes(handles.axes1);x=impulse(b,a);plot(x,linewidth,2); grid on; hold on;function pushbutton2_callback(hobject, eventdata, handles)m= str2num(get(handles.rlc,string)r=m(1);l=m(2);c=m(3); a=l*c r*c 1; b=1;y=step(b,a);axes(handles.axes2)plot(y,linewidth,2); grid on; hold on;把上述初始化程序及回调函数加入表1 的相应位置,然后运行程序,并输入r、l、c 的值(若分别键入1 3 1),按下“冲激响应”按钮,则输出冲激响应波形,按下“阶跃响应”按钮,则输出阶跃响应波形,运行结果如图2 所示。图23 结束语通过对matlab guide 图形用户界面的设计实例,展示了人机交互式图形用户界面及其参数的可调控性。在实际设计制作中要解决的几个关键技术问题是:利用各控件的属性进行数据的保存、传递;通过句柄访问控件并进行数据的获取和传递;编写各控件的回调函数。回调函数的编写不可能一蹴而就,需要反复的调试、修改和运行才能够使程序得以正确运行。附录:test.m文件编程语句function varargout = test4(varargin)% test4 matlab code for test4.fig% test4, by itself, creates a new test4 or raises the existing% singleton*.% h = test4 returns the handle to a new test4 or the handle to% the existing singleton*.% test4(callback,hobject,eventdata,handles,.) calls the local% function named callback in test4.m with the given input arguments.% test4(property,value,.) creates a new test4 or raises the% existing singleton*. starting from the left, property value pairs are% applied to the gui before test4_openingfcn gets called. an% unrecognized property name or invalid value makes property application% stop. all inputs are passed to test4_openingfcn via varargin.% *see gui options on guides tools menu. choose gui allows only one% instance to run (singleton).% see also: guide, guidata, guihandles % rlc the above text to modify the response to help test4 % last modified by guide v2.5 12-jan-2013 21:05:56 % begin initialization code - do not rlcgui_singleton = 1;gui_state = struct(gui_name, mfilename, . gui_singleton, gui_singleton, . gui_openingfcn, test4_openingfcn, . gui_outputfcn, test4_outputfcn, . gui_layoutfcn, , . gui_callback, );if nargin & ischar(varargin1) gui_state.gui_callback = str2func(varargin1);end if nargout varargout1:nargout = gui_mainfcn(gui_state, varargin:);else gui_mainfcn(gui_state, varargin:);end% end initialization code - do not rlc % - executes just before test4 is made visible.function test4_openingfcn(hobject, eventdata, handles, varargin)i=imread(dl.bmp,bmp);%dlaxes(handles.axes10); %axes10image(i);axis off % this function has no output args, see outputfcn.% hobject handle to figure% eventdata reserved - to be defined in a future version of matlab% handles structure with handles and user data (see guidata)% varargin command line arguments to test4 (see varargin) % choose default command line output for test4handles.output = hobject; % update handles structureguidata(hobject, handles); % uiwait makes test4 wait for user response (see uiresume)% uiwait(handles.figure1); % - outputs from this function are returned to the command line.function varargout = test4_outputfcn(hobject, eventdata, handles) % varargout cell array for returning output args (see varargout);% hobject handle to figure% eventdata reserved - to be defined in a future version of matlab% handles structure with handles and user data (see guidata) % get default command line output from handles structurevarargout1 = handles.output; % - executes on button press in pushbutton1.function pushbutton1_callback(hobject, eventdata, handles)m=str2num(get(handles.edit5,string)r=m(1);l=m(2);c=m(3);a=l*c r*c 1;b=1;axes(handles.axes1);x=impulse(b,a);plot(x,linewidth,2);grid on;hold on;% hobject handle to pushbutton1 (see gcbo)% eventdata reserved - to be defined in a future version of matlab% handles structure with handles and user data (see guidata) % - executes on button press in pushbutton2.function pushbutton2_callback(hobject, eventdata, handles)m=str2num(get(handles.edit5,string)r=m(1);l=m(2);c=m(3);a=l*c r*c 1;b=1;y=step(b,a);axes(handles.axes2);plot(y,linewidth,2);grid on;hold on;% hobject handle to pushbutton2 (see gcbo)% eventdata reserved - to be defined in a future version of matlab% handles structure with handles and user data (see guidata) function rlc_callback(hobject, eventdata, handles)% hobject handle to rlc (see gcbo)% eventdata reserved - to be defined in a future version of matlab% handles structure with handles and user data (see guidata) % hints: get(hobject,string) returns contents of rlc as text% str2double(get(hobject,string) returns contents of rlc as a double % - executes during object creation, after setting all properties.function rlc_createfcn(hobject, eventdata, handles)set(gcbo,string,1 1 1); %rlc% hobject handle to rlc (see gcbo)% eventdata reserved - to be defined in a future version of matlab% handles empty - handles not created until after all createfcns called % hint: rlc controls usually have a white background on windows.% see ispc and computer.if ispc & isequal(get(hobject,backgroundcolor), get(0,defaultuicontrolbackgroundcolor) set(hobject,backgroundcolor,white);end function edit5_callback(hobject, eventdata, handles)% hobject handle to edit5 (see gcbo)% eventdata reserved - to be defined in a future version of matlab% handles structure with handles and user data (see guidata) % hints: get(hobject,string) returns contents of edit5 as text% str2double(get(hobject,string) returns contents of edit5 as a double % - executes during object creation, after setting all properties.function edit5_createfcn(hobject, eventdata, handles)% hobject handle to edit5 (see gcbo)% eventdata reserved - to be defined in a future version of matlab% handles empty - handles not created until after all createfcns called % hint: edit controls usually have a white background on windows.% see ispc and computer.if ispc & isequal(get(hobject,backgroundcolor), get(0,defaultuicontrolbackgroundcolor) set(hobject,backgroundcolor,white);end % - executes on button press in pushbutton3.function pushbutton3_callback(hobject, eventdata, handles)filename,pathname=uiputfile(*.bmp;,save,undefined.bmp);if isequal(filename,0) str = pathname filename; px=getframe(handles.axes1); %saveas(gcf,str,bmp); ta = getappdata(gc
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 设施设备维护管理制度
- 设计食堂进出管理制度
- 诊所人员培训管理制度
- 诊所熬药日常管理制度
- 试剂耗材节约管理制度
- 财务资金规范管理制度
- 财富管理平台管理制度
- 货物搬运装卸管理制度
- 货物配送薪酬管理制度
- 货车安全培训管理制度
- 深圳市光明区智慧城市专项规划
- GB/T 19598-2025地理标志产品质量要求安溪铁观音
- 施工现场安全防护标准化图集
- 城区建筑垃圾处理资源再利用设备采购 投标方案(技术方案)
- 国家教育考试保密安全培训
- 《国际商事调解》课件
- T-ZJICA 1101-2024 算力中心智慧物业服务规范
- 罐车卸车安全操作规程
- DG-TG08-12-2024 普通中小学建设标准
- 招牌字施工合同
- 淋球菌基因表达调控-深度研究
评论
0/150
提交评论