Matlab GUI 菜单设计

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Matlab GUI 菜单设计相关的知识,希望对你有一定的参考价值。

界面布局的设计我已经会了,关键是如何实现在界面的框架下的菜单选项的回调,即当点击菜单的某个选项后有相应的操作!比方说点击“打开”,可以打开任意文件下的图像,有程序的可以帮忙吗

参考技术A 比方说点击“打开”,可以打开任意文件下的图像,有程序的可以帮忙吗 ??
% --- 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)
global im

[filename pathname] = ...
uigetfile('*.jpg';'*.bmp';'*.gif', '开始');

str=[pathname filename];

im=imread(str);
axes(handles.axes1);
imshow(im);本回答被提问者和网友采纳
参考技术B 点击菜单的某个选项后有相应的操作
你在菜单编辑器中增加一个菜单后,界面上会有相对应的Tag,下面有个Callback和一空行,点击空行右边的View即可进入M文件编辑器,在下面编程即可。

matlab gui 设计 日志

下面是我自己做的matlab环境下的图形处理小工具。

聊生于无。配合Guid一起用。

function varargout = dazuoye(varargin)
% DAZUOYE MATLAB code for dazuoye.fig
%      DAZUOYE, by itself, creates a new DAZUOYE or raises the existing
%      singleton*.
%
%      H = DAZUOYE returns the handle to a new DAZUOYE or the handle to
%      the existing singleton*.
%
%      DAZUOYE(‘CALLBACK‘,hObject,eventData,handles,...) calls the local
%      function named CALLBACK in DAZUOYE.M with the given input arguments.
%
%      DAZUOYE(‘Property‘,‘Value‘,...) creates a new DAZUOYE or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before dazuoye_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to dazuoye_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 dazuoye

% Last Modified by GUIDE v2.5 10-May-2020 23:43:01

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct(‘gui_Name‘,       mfilename, ...
                   ‘gui_Singleton‘,  gui_Singleton, ...
                   ‘gui_OpeningFcn‘, @dazuoye_OpeningFcn, ...
                   ‘gui_OutputFcn‘,  @dazuoye_OutputFcn, ...
                   ‘gui_LayoutFcn‘,  [] , ...
                   ‘gui_Callback‘,   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before dazuoye is made visible.
function dazuoye_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 dazuoye (see VARARGIN)

% Choose default command line output for dazuoye
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

set(handles.pushbutton1,‘userdata‘,0);
set(handles.text2,‘userdata‘,0); 
set(handles.popupmenu1,‘userdata‘,0);

% UIWAIT makes dazuoye wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = dazuoye_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 structure
varargout{1} = handles.output;


% --- 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,  filepath]=uigetfile(‘*.jpg;*.png;*.tif;*.gif;*.img‘,‘请选择图片‘);
str=[filepath,  filename];
if ~isequal(str,[0,0])
    [ a , b , c ]=fileparts(filename);
    if((strcmp(c ,‘.jpg‘))||(strcmp(c ,‘.png‘))||(strcmp(c ,‘.tif‘))||(strcmp(c ,‘.gif‘))||(strcmp(c ,‘.img‘)))
    ima =  imread(str);
    else
      warndlg(‘请选择正确格式的图片!‘); 
      return 
    end
else
   warndlg(‘请选择图片!‘); 
   return
end
axes(handles.axes1);
%subplot(2,3,1);
imshow(ima);
title(‘原始图像‘);
set(handles.pushbutton1,‘userdata‘,ima);
axes(handles.axes2);
imshow(ima);
title(‘无修改‘);
set(handles.text2,‘userdata‘,ima); 
set(handles.popupmenu1,‘userdata‘,ima);


% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(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
comtents={‘无‘,‘素描风格‘,‘浮雕风格‘,‘胶片滤镜‘,‘(伪)红外滤镜‘,‘图像反色‘,‘边缘风格‘};
 I=get(handles.pushbutton1,‘userdata‘);
if(I==0)
  warndlg(‘未选择图片!‘);
  return
end   
if(strcmp(comtents(get(hObject,‘Value‘)),‘无‘))
  % I=get(handles.pushbutton1,‘userdata‘);
   axes(handles.axes2);
   imshow(I);
   title(‘无修改‘);
   set(handles.popupmenu1,‘userdata‘,I);
   set(handles.text2,‘userdata‘,I); 
end
if(strcmp(comtents(get(hObject,‘Value‘)),‘素描风格‘))
   % I=get(handles.pushbutton1,‘userdata‘); 
    info_size=size(I);  
    height=info_size(1);  
    width=info_size(2);  
    N=zeros(height,width);  
    g=zeros(height,width);
  if(ndims(I)==3)
    imggray=rgb2gray(I);
  else
    imggray=I;
  end
    out=zeros(height,width); 
    spec=zeros(height,width,3);  
for I=1:height  
    for j=1:width  
          
        N(I,j)=255-imggray(I,j);  
          
    end  
end  
  
for I=2:height-1  
    for j=2:width-1  
        sum=0;  
        sum=1*double(N(I-1,j-1))+2*double(N(I-1,j))+1*double(N(I-1,j+1));  
        sum=sum+2*double(N(I,j-1))+4*double(N(I,j))+2*double(N(I,j+1));  
        sum=sum+1*double(N(I+1,j-1))+2*double(N(I+1,j))+1*double(N(I+1,j+1));  
        sum=sum/16;  
        g(I,j)=sum;  
    end  
end  
  
for I=1:height  
    for j=1:width  
        b=double(g(I,j));  
        a=double(imggray(I,j));  
        temp=a+a*b/(256-b);  
        out(I,j)=uint8(min(temp,255));  
          
    end  
end  
%subplot(2,3,2);
    ima=out/255;
    axes(handles.axes2);
    imshow(ima);
    title(‘素描风格‘);  
    set(handles.popupmenu1,‘userdata‘,ima);        
    set(handles.text2,‘userdata‘,ima); 
end
if(strcmp(comtents(get(hObject,‘Value‘)),‘浮雕风格‘))
   % I=get(handles.pushbutton1,‘userdata‘);
   if(ndims(I)==3)
    I=rgb2gray(I);
   end
    model=fspecial(‘prewitt‘);
    ima=filter2(model,I);
    %subplot(2,3,2);
    axes(handles.axes2);
    imshow(ima);
    title(‘浮雕风格‘);
    set(handles.popupmenu1,‘userdata‘,ima); 
    set(handles.text2,‘userdata‘,ima); 
end
if(strcmp(comtents(get(hObject,‘Value‘)),‘胶片滤镜‘))
   % I=get(handles.pushbutton1,‘userdata‘);
   if(ndims(I)~=3)
       warndlg(‘请选择RGB图片‘);
       return
   end
    R=I(:,:,1);
    G=I(:,:,2);
    B=I(:,:,3);
    r=0.393 * R + 0.769 * G + 0.189 * B;
    g=0.349 * R + 0.686 * G + 0.168 * B;
    b=0.272 * R + 0.534 * G + 0.131 * B;
    imageo(:,:,1)=r;
    imageo(:,:,2)=g;
    imageo(:,:,3)=b;
    %subplot(2,3,2);
    axes(handles.axes2);
    imshow(imageo);
    title(‘胶片滤镜‘);
    set(handles.popupmenu1,‘userdata‘,imageo); 
    set(handles.text2,‘userdata‘,imageo); 
end
if(strcmp(comtents(get(hObject,‘Value‘)),‘(伪)红外滤镜‘))
  % I=get(handles.pushbutton1,‘userdata‘);
   if(ndims(I)==3)
   I = rgb2gray(I);
   end
   I = double(I); 
   [m,n] = size(I); 
   c = 256; 
for i = 1:m     
    for j = 1:n 
        if I(i,j) <= c/4     
            R(i,j) = 0;    
            G(i,j) = 4 * I(i,j);   
            B(i,j) = c; 
        else if I(i,j) <= c/2    
            R(i,j) = 0;      
            G(i,j) = c;         
            B(i,j) = -4 * I(i,j) + 2 * c;     
            else if I(i,j) <= 3 * c/4            
                    R(i,j) = 4 * I(i,j)-2 * c;             
                    G(i,j) = c;           
                    B(i,j) = 0;        
                else
                    R(i,j) = c;          
                    G(i,j) = -4 * I(i,j) + 4 * c;     
                    B(i,j) = 0;       
                end
            end
        end
    end
end
for i = 1:m    
    for j = 1:n   
        out(i,j,1) = R(i,j);  
        out(i,j,2) = G(i,j);    
        out(i,j,3) = B(i,j);   
    end
end
out = out/256; 
axes(handles.axes2);
imshow(out);
set(handles.popupmenu1,‘userdata‘,out); 
set(handles.text2,‘userdata‘,out); 
end
if(strcmp(comtents(get(hObject,‘Value‘)),‘图像反色‘))
 % I=get(handles.pushbutton1,‘userdata‘);
  I=255-I;
  axes(handles.axes2);
  imshow(I);
  set(handles.popupmenu1,‘userdata‘,I); 
  set(handles.text2,‘userdata‘,I); 
end
if(strcmp(comtents(get(hObject,‘Value‘)),‘边缘风格‘))
 % I=get(handles.pushbutton1,‘userdata‘);
  if(ndims(I)==3)
    I=rgb2gray(I);
  end
  EG=edge(I,‘canny‘);
  axes(handles.axes2);
  imshow(EG);
  set(handles.popupmenu1,‘userdata‘,EG);
  set(handles.text2,‘userdata‘,EG);
end

% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(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



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
I=get(handles.popupmenu1,‘userdata‘);
if(I==0)
    warndlg(‘未选择图片!‘);
    return
end
j=str2double(get(hObject,‘string‘));
I=imrotate(I,j);
axes(handles.axes2);
imshow(I);
title(‘旋转效果‘);
set(handles.edit1,‘userdata‘,I);
set(handles.text2,‘userdata‘,I); 

% --- 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


% --- 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)
  I=get(handles.text2,‘userdata‘);
  if(I==0)
      warndlg(‘未选择图片!‘);
      return
  end
  I=flip(I,2);
  axes(handles.axes2);
  imshow(I);
  set(handles.text2,‘userdata‘,I);


% --- Executes on button press in pushbutton3.
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)
[filename, pathname]=uiputfile({‘*.jpg‘;‘*.png‘;‘*.img‘;‘*.tif‘;‘*.gif‘},‘保存当前图片‘,‘untitled‘);
str=[pathname filename];
if(filename==0)
    if(pathname~=0)
        warndlg(‘请输入文件名!‘);
        return
    end
else
    I=get(handles.text2,‘userdata‘);
    if(I==0)
        warndlg(‘未选择图片!‘);
        return
    end
    imwrite(I,str);
end

  

以上是关于Matlab GUI 菜单设计的主要内容,如果未能解决你的问题,请参考以下文章

matlab GUI界面自定义菜单的设置,怎么设它的回调函数?

如何用MATLAB GUI创建图形用户界面

matlab怎么修改编辑好的gui

急急急!!!matlab gui界面设计

在 MATLAB GUI 弹出菜单中写入指数

matlab gui界面设计日历用啥编辑器