一个matlab GUI使用例子,pop-up menu的用法

一个matlab GUI使用例子,pop-up menu的用法

本例子要实现的GUI功能是这样的,从一个EXCEL文件中读取数据,并将数据分类,作图的算例。数据按照下图中的Haem和avscr的不同数据进行分类。

EXCEL中的内容大概是这样的如下图所示,共有1000多行。手动解决确实比较费力。

一个matlab GUI使用例子,pop-up menu的用法

一个简单的matlab 的gui程序包含两个文件,一个是.fig文件,一个是.m文件,两个文件是有相同的名字的。

首先来看.fig文件如何建立。

下面打开matlab的guide,命令行直接键入guide回车就好
一个matlab GUI使用例子,pop-up menu的用法

选择default就好,点击OK。

一个matlab GUI使用例子,pop-up menu的用法

如下图所示,将各个部件添加进去

一个matlab GUI使用例子,pop-up menu的用法

做好后就是下图这个效果
一个matlab GUI使用例子,pop-up menu的用法

稍微有点丑,各位可以自己美化一下哈

Pop-up Menu双击后出现的对话框中的string项要设置好,需要用户有哪几种选择。

下一步就是最重要的一步,给各个按钮神马的编写code

由于按钮较多,本文主要写一下Pop-up Menu的Callback编写方法

代码如下,就可以得到当选择了某个项时,得到的字符串是什么。用的switch结构。

str = get(hObject, ‘String’);  
val = get(hObject,’Value’);  
% Set current data to the selected data set.
switch str{val};  
case ‘1e-6’  
   handles.Haem = 1e-6;
case ‘3e-6’  
   handles.Haem = 3e-6;
case ‘5e-6’  
   handles.Haem = 5e-6;
case ‘7e-6’  
   handles.Haem = 7e-6;
case ‘9e-6’  
   handles.Haem = 9e-6;
end  
guidata(hObject,handles)%这一句非常重要,如果没有这一句,句柄handles.Haem是无法更新的  

附上m文件的全文

function varargout = Haemav(varargin)  
%HAEMAV M-file for Haemav.fig
%      HAEMAV, by itself, creates a new HAEMAV or raises the existing
%      singleton.
%
%      H = H_AEM_AV returns the handle to a new H_AEM_AV or the handle to
%      the existing singleton.
%
%      HAEMAV(‘Property’,’Value’,…) creates a new HAEMAV using the
%      given property value pairs. Unrecognized properties are passed via
%      varargin to HaemavOpeningFcn.  This calling syntax produces a
%      warning when there is an existing singleton*.
%
%      HAEMAV(‘CALLBACK’) and HAEMAV(‘CALLBACK’,hObject,…) call the
%      local function named CALLBACK in HAEMAV.M with the given input
%      arguments.
%
%      *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 Haemav
% Last Modified by GUIDE v2.5 27-Nov-2014 21:22:17
% Begin initialization code – DO NOT EDIT
guiSingleton = 1;  
guiState = struct(‘guiName’,       mfilename, …  
                   ‘guiSingleton’,  guiSingleton, …
                   ‘guiOpeningFcn’, @HaemavOpeningFcn, …
                   ‘guiOutputFcn’,  @HaemavOutputFcn, …
                   ‘guiLayoutFcn’,  [], …
                   ‘guiCallback’,   []);
if nargin && ischar(varargin{1})  
   guiState.guiCallback = str2func(varargin{1});
end  
if nargout  
    [varargout{1:nargout}] = guimainfcn(guiState, varargin{:});
else  
    guimainfcn(guiState, varargin{:});
end  
% End initialization code – DO NOT EDIT
% — Executes just before Haemav is made visible.
function HaemavOpeningFcn(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   unrecognized PropertyName/PropertyValue pairs from the
%            command line (see VARARGIN)
% Choose default command line output for Haemav
handles.output = hObject;  
% Update handles structure
guidata(hObject, handles);  
handles.Haem=1e-6;  
guidata(hObject,handles)  
handles.av=1e6;  
guidata(hObject,handles)  
[handles.num,handles.txt] = xlsread(‘C:UsersJianDocumentsMATLABHaem-avscr.xls’,’Haem-avscr’);%读取excel表格sheet为Haem-avscr的内容
guidata(hObject,handles)  
% UIWAIT makes Haemav wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% — Outputs from this function are returned to the command line.
function varargout = HaemavOutputFcn(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 structure
varargout{1} = handles.output;  
% — Executes on selection change in popupmenu1.
function popupmenu1Callback(hObject, eventdata, handles)  
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved – to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,’String’)) returns popupmenu1 contents as cell array
%        contents{get(hObject,’Value’)} returns selected item from popupmenu1
str = get(hObject, ‘String’);  
val = get(hObject,’Value’);  
% Set current data to the selected data set.
switch str{val};  
case ‘1e-6’ % User selects peaks.  
   handles.Haem = 1e-6;
case ‘3e-6’ % User selects membrane.  
   handles.Haem = 3e-6;
case ‘5e-6’ % User selects sinc.  
   handles.Haem = 5e-6;
case ‘7e-6’ % User selects peaks.  
   handles.Haem = 7e-6;
case ‘9e-6’ % User selects membrane.  
   handles.Haem = 9e-6;
end  
guidata(hObject,handles)  
% — Executes on selection change in popupmenu2.
function popupmenu2Callback(hObject, eventdata, handles)  
% hObject    handle to popupmenu2 (see GCBO)
% eventdata  reserved – to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,’String’)) returns popupmenu2 contents as cell array
%        contents{get(hObject,’Value’)} returns selected item from popupmenu2
str = get(hObject, ‘String’);  
val = get(hObject,’Value’);  
% Set current data to the selected data set.
display(val);  
switch str{val};  
case ‘1e6’ % User selects peaks.  
   handles.av = 1e6;
case ‘1e7’ % User selects membrane.  
   handles.av = 1e7;
case ‘1e8’ % User selects sinc.  
   handles.av = 1e8;
case ‘1e9’ % User selects peaks.  
   handles.av = 1e9;
case ‘1e10’ % User selects membrane.  
   handles.av = 1e10;
end  
guidata(hObject,handles)  
% — Executes on button press in pushbutton2.
function pushbutton2Callback(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)
[~,coli]=find(strncmp(handles.txt, ‘Electrolyte current density norm (A/m^2), i electrolyte’,50));
[~,colvcell]=find(strncmp(handles.txt, ‘% Vcell’,8));
[~,colHaem]=find(strncmp(handles.txt, ‘Haem’,5));
[~,colav]=find(strncmp(handles.txt, ‘avscr’,6));
data(:,1)=handles.num(:,colav);%这一列是厚度m  
data(:,2)=handles.num(:,colHaem);%这一列是湿度  
data(:,3)=handles.num(:,colvcell);%这一列是输出电压(V)  
data(:,4)=handles.num(:,coli);%这一列是电流  
data(:,4)=data(:,4)./10;%这一列是将电流从A/m^2换算为mA/cm^2  
data(:,5)=data(:,3).data(:,4);%功率(mW/cm^2)  
%display(data);
[a,~]=find(data(:,2)<(handles.H_aem(1.+0.01))&data(:,2)>(handles.Haem*(1.-0.01)));
temp=data(a,:);  
%assignin(‘base’, ‘temp’, temp);
%display(temp);
[b,~]=find(temp(:,1)<(handles.av*(1+0.01))&temp(:,1)>(handles.av*(1-0.01)));
temp1=temp(b,:);  
%assignin(‘base’, ‘temp1’, temp1);
%display (temp1);
[~,hLine1,hLine2] = plotyy(handles.axes1,temp1(:,4),temp1(:,3),temp1(:,4),temp1(:,5));
display(handles.av);  
display(handles.Haem);  
set(hLine1,’LineStyle’,’–‘)  
set(hLine1,’Marker’,’o’)  
set(hLine2,’LineStyle’,’:’)  
set(hLine2,’Marker’,’*’)  
clear temp1;  
clear temp;  
% — Executes during object creation, after setting all properties.
function popupmenu2CreateFcn(hObject, eventdata, handles)  
% hObject    handle to popupmenu2 (see GCBO)
% eventdata  reserved – to be defined in a future version of MATLAB
% handles    empty – handles not created until after all CreateFcns called
% Hint: popupmenu 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 during object creation, after setting all properties.
function popupmenu1CreateFcn(hObject, eventdata, handles)  
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved – to be defined in a future version of MATLAB
% handles    empty – handles not created until after all CreateFcns called
% Hint: popupmenu 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 during object creation, after setting all properties.
function axes1_CreateFcn(hObject, eventdata, handles)  
% hObject    handle to axes1 (see GCBO)
% eventdata  reserved – to be defined in a future version of MATLAB
% handles    empty – handles not created until after all CreateFcns called
% Hint: place code in OpeningFcn to populate axes1

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据