600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > MATLAB GUI创建图形用户界面/交互界面

MATLAB GUI创建图形用户界面/交互界面

时间:2023-06-22 12:57:07

相关推荐

MATLAB GUI创建图形用户界面/交互界面

目录

目标:根据自己写的函数,写一个交互式界面。

第一步:命令窗口输入guide,得到 “.fig” 和 “.m” 文件

第二步:设计界面

第三步:编写界面中元素对应代码

第四步 结果

参考资料

目标:根据自己写的函数,写一个交互式界面。

其实整体较简单,关键是理解整个MATLAB GUI 运行的逻辑!

PS 一切将简单问题复杂化的东西,真的可恨!

第一步:命令窗口输入guide,得到 “.fig” 和 “.m” 文件

第二步:设计界面

第三步:编写界面中元素对应代码

上述界面框对应的代码如下:

%%%%%%%%%%%%%%%%%%%%%%%%%%% 主函数 %%%%%%%%%%%%%%%%%%%%%%%%%%function varargout = Example_test(varargin)% EXAMPLE_TEST MATLAB code for Example_test.fig%EXAMPLE_TEST, by itself, creates a new EXAMPLE_TEST or raises the existing%singleton*.%%H = EXAMPLE_TEST returns the handle to a new EXAMPLE_TEST or the handle to%the existing singleton*.%%EXAMPLE_TEST('CALLBACK',hObject,eventData,handles,...) calls the local%function named CALLBACK in EXAMPLE_TEST.M with the given input arguments.%%EXAMPLE_TEST('Property','Value',...) creates a new EXAMPLE_TEST or raises the%existing singleton*. Starting from the left, property value pairs are%applied to the GUI before Example_test_OpeningFcn gets called. An%unrecognized property name or invalid value makes property application%stop. All inputs are passed to Example_test_OpeningFcn via varargin.%%*See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one%instance to run (singleton)".%% See also: GUIDE, GUIDATA, GUIHANDLES% Edit the above text to modify the response to help Example_test% Last Modified by GUIDE v2.5 07-Aug- 15:16:01% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct('gui_Name', mfilename, ...'gui_Singleton', gui_Singleton, ...'gui_OpeningFcn', @Example_test_OpeningFcn, ...'gui_OutputFcn', @Example_test_OutputFcn, ...'gui_LayoutFcn', [] , ...'gui_Callback', []);if nargin && ischar(varargin{1})gui_State.gui_Callback = str2func(varargin{1});endif nargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});elsegui_mainfcn(gui_State, varargin{:});end% End initialization code - DO NOT EDIT%%%%%%%%%%%%%%%%%%%%%%%%%%% openingfucntion %%%%%%%%%%%%%%%%%%%%%%%%%%% --- Executes just before Example_test is made visible.function Example_test_OpeningFcn(hObject, eventdata, handles, varargin)% 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 Example_test (see VARARGIN)% Choose default command line output for Example_testhandles.output = hObject;% Update handles structureguidata(hObject, handles);% UIWAIT makes Example_test wait for user response (see UIRESUME)% uiwait(handles.figure1);%%%%%%%%%%%%%%%%%%%%%%%%%%% output function %%%%%%%%%%%%%%%%%%%%%%%%%%% --- Outputs from this function are returned to the command line.function varargout = Example_test_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 structurevarargout{1} = handles.output;%%%%%%%%%%%%%%%%%%%%%%%%%%% 编辑文本1 %%%%%%%%%%%%%%%%%%%%%%%%%%function edit1_Callback(hObject, eventdata, handles)% hObject handle to edit1 (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 edit1 as text% str2double(get(hObject,'String')) returns contents of edit1 as a double% --- Executes during object creation, after setting all properties.function edit1_CreateFcn(hObject, eventdata, handles)% hObject handle to edit1 (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%%%%%%%%%%%%%%%%%%%%%%%%%%% 编辑文本2 %%%%%%%%%%%%%%%%%%%%%%%%%%function edit2_Callback(hObject, eventdata, handles)% hObject handle to edit2 (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 edit2 as text% str2double(get(hObject,'String')) returns contents of edit2 as a double% --- Executes during object creation, after setting all properties.function edit2_CreateFcn(hObject, eventdata, handles)% hObject handle to edit2 (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%%%%%%%%%%%%%%%%%%%%%%%%%%% 按钮1 %%%%%%%%%%%%%%%%%%%%%%%%%%% --- Executes on button press in pushbutton1.function pushbutton1_Callback(hObject, eventdata, handles)% 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)[Filename, Pathname]=uigetfile('*.txt;*.xlsx','Select Input Ground Motion'); % Raw download PEER datafile=[Pathname,Filename];data=importdata(file);handles.data=data; % 不同子函数之间进行数据传输axes(handles.axes1);plot(data,'k','linewidth',1,'Color',[0.7 0.7 0.7]);guidata(hObject,handles)%%%%%%%%%%%%%%%%%%%%%%%%%%% 按钮2 %%%%%%%%%%%%%%%%%%%%%%%%%%% --- Executes on button press in pushbutton2.function pushbutton2_Callback(hObject, eventdata, handles)% 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)thres=get(handles.edit1,'string');thres=str2double(thres);data=handles.data;data=data(data>thres);axes(handles.axes2);plot(data,'k','linewidth',1,'Color',[0.7 0.7 0.7]);%%%%%%%%%%%%%%%%%%%%%%%%%%% 将结果写入 编辑文档2 %%%%%%%%%%%%%%%%%%%%%%%%%%S=data(1:2,1);C=num2str(S'); % 行向量!set(handles.edit2,'String',C)

主要注意如下几点:

(1)核心是写“按钮”和“可编辑文档”对应的callback部分,其他(如主函数,opening function, output function)不需要动!

(2)了解不同子函数间数据是如何传递的,本文中采用的代码如下,其他方式详见Matlab的GUI参数传递方式总结。

data=importdata(file);handles.data=data; % 不同子函数之间进行数据传输guidata(hObject,handles) % 在子函数最后,一定要加上这句!

(3)如何画图,本文采用的代码如下:

axes(handles.axes1);plot(data,'k','linewidth',1,'Color',[0.7 0.7 0.7]);

(4)如何清空图形和“可编辑文本”内的文字,代码如下

function pushbutton3_Callback(hObject, eventdata, handles)% hObject handle to pushbutton3 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)trydelete(allchild(handles.axes1));endset(handles.edit1,'String','');

(5)理解“get函数”和“set函数”在GUI中的意义!

(6)一个文本框内放入向量,思路是将向量变成字符串,注意得是行向量!

S=data(1:2,1);C=num2str(S'); % 行向量!set(handles.edit2,'String',C)

第四步 结果

参考资料

[1]Matlab GUI界面设计

[2]matlab GUI界面编程总结

[3]matlab编程GUI基础

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。