chengweifeng 发表于 2007-8-15 21:51:10

gui之基础篇(m-file)

很多书本上对于gui的实例讲解,尤其是完整的例题讲解极少,大都是对某个功能先是理论一翻,然后给一个没头没脑的几句代码!!这让很多新手看后仍然不知如何去具体编写自己的程序.而网上一些高手的程序,有时高深莫测,让新手一头雾水,希望本帖能为如我之新手者提供一个归宿!:lol

这个程序包含了动态扩展菜单项目,子菜单以及文件名处理对话框(点击open可见)的实现.

function myguide
% myguideGui programm
%    cheng wf2007 8 15 19:04
%
screensize=get(0,'screensize'); %以pixels为单位
%设置图标
L = 40*membrane(1,25);
newwin=figure('name','Matlab/made in china/cheng wf','Color',,'position',...
    [(screensize(3)-300)*0.5,(screensize(4)-200)*0.5,600,400],'numbertitle','off','menubar','none');
logoax = axes('CameraPosition', [-193.4013 -265.1546220.4819],...
    'CameraTarget',,'CameraUpVector',, ...
    'CameraViewAngle',9.5, ...
    'DataAspectRatio', ,...
    'Position',, ...
    'Visible','off', ...
    'XLim',, ...
    'YLim',, ...
    'ZLim',[-13 40], ...
    'parent',newwin);
s = surface(L, ...
    'EdgeColor','none', ...
    'FaceColor',, ...
    'FaceLighting','phong', ...
    'AmbientStrength',0.3, ...
    'DiffuseStrength',0.6, ...
    'Clipping','off',...
    'BackFaceLighting','lit', ...
    'SpecularStrength',1.1, ...
    'SpecularColorReflectance',1, ...
    'SpecularExponent',7, ...
    'Tag','TheMathWorksLogo', ...
    'parent',logoax);
l1 = light('Position',, ...
    'Style','local', ...
    'Color',, ...
    'parent',logoax);
l2 = light('Position',[.5 -1 .4], ...
    'Color',, ...
    'parent',logoax);
%下拉菜单的实现
filemenu=uimenu(newwin,'label','&File');
editmenu=uimenu(newwin,'label','&Edit');
viewmenu=uimenu(newwin,'label','&View');
helpmenu=uimenu(newwin,'label','&Help');
itemnew=uimenu(filemenu,'label','&New');
itemopen=uimenu(filemenu,'label','&Open...');
%设置文件名处理对话框
set(itemopen,'callback',['=uigetfile( ''*.txt'',''Open file'',200,400);']);
%设置下拉菜单项目
itemsave=uimenu(filemenu,'label','&Save','Separator','on'); %设置各组菜单项目之间的分隔符
itemsaveas=uimenu(filemenu,'label','&Save&As');
itemexit=uimenu(filemenu,'label','&Exit','Separator','on');
itemundo=uimenu(editmenu,'label','&Undo');
itemredo=uimenu(editmenu,'label','&Redo');
itemcut=uimenu(editmenu,'label','&Cut','separator','on');
itemcopy=uimenu(editmenu,'label','&Copy');
itempaste=uimenu(editmenu,'label','&Paste');
itemselect=uimenu(editmenu,'label','&Select&All','separator','on');
itemdesktoplayout=uimenu(viewmenu,'label','Desktop&Layout');
itemundockCW=uimenu(viewmenu,'label','&Undock command window');
itemcommandwindow=uimenu(viewmenu,'label','Command window','separator','on');
itemcommandhistory=uimenu(viewmenu,'label','Command history');
itemindex=uimenu(helpmenu,'label','&Matlab Help');
itemcontent=uimenu(helpmenu,'label','&About Matlab');
%子菜单
itemmfile=uimenu(itemnew,'label','m_file');
itemfigure=uimenu(itemnew,'label','&Figure');
itemmodel=uimenu(itemnew,'label','&Model');
itemGUI=uimenu(itemnew,'label','&GUI');
%动态扩展
set(itemundo,'Enable','off');
set(itempaste,'Enable','off');

申明:程序中从"%设置图标-->%下拉菜单的实现"大约30句代码实在记不清楚是从哪里下的了!
   也许是mathwork.com,或者论坛里某位高人的代码.如有冒犯,还望海涵.同时,向你致谢.别无他
   意,纯属视觉美观.

由于小弟刚学gui也就一个星期左右.程序不免许多幼稚之处,望高手多多指教!!

[ 本帖最后由 chengweifeng 于 2007-8-27 11:38 编辑 ]

chengweifeng 发表于 2007-8-16 19:49:09

原来matlab带了这个函数,哪位高手可否帮小弟解释一下下面红色语句的作用!
function splashdemo
% SPLASHDEMO   Plot the L-shaped membrane with semitransparent color.
%   Copyright 1984-2002 The MathWorks, Inc.
%   $Revision: 1.3 $$Date: 2002/04/08 20:04:53 $
L = 40*membrane(1,25);
% Generate a circular alphadata for the mesh.
= meshgrid(1:size(L,1));
alphadata = sqrt((i-4).^2 + .8*(j+4).^2);
% This alphamap causes there to be a thin band that
% is semi-transparent, as opposed to the entire surface
fig = figure;
set(fig, 'Color', 'white',...
'AlphaMap',...
[ zeros(1,40), ...
    linspace(0,.5,15), linspace(.5,1,8), ...
    ones(1,30)]);
axes('CameraPosition', [-193.4013 -265.1546220.4819],...
   'CameraTarget',, ...
   'CameraUpVector',, ...
   'CameraViewAngle',9.5, ...
   'DataAspectRatio', ,...
   'Position',, ...
   'Visible','off', ...
   'XLim',, ...
   'YLim',, ...
   'ZLim',[-13 40]);
% This surface represents a white opaque surface with edge lines.
s1 = surface(L, ...
      'FaceColor','white', ...
      'FaceLighting','none', ...
      'Clipping','off',...
      'EdgeColor',[.8 .8 .8]);
% This represends the colored surface.
% The alpha map will let the white surface show through.
% The FaceOffset* properties force it to be closer to the camera
%   than the edge lines of the white surface
s2 = surface(L, ...
      'FaceColor',, ...
      'FaceLighting','phong', ...
      'AmbientStrength',0.3, ...
      'DiffuseStrength',0.6, ...
      'Clipping','off',...
      'BackFaceLighting','lit', ...
      'SpecularStrength',1.1, ...
      'SpecularColorReflectance',1, ...
      'SpecularExponent',7, ...
      'EdgeColor','none', ...
      'FaceOffsetFactor',-1.5,...
      'FaceOffsetBias',-0.001,...
      'FaceAlpha','interp', ...
      'AlphaData',flipud(alphadata));
l1 = light('Position',, ...
    'Style','local', ...
    'Color',);
l2 = light('Position',[.5 -1 .4], ...
    'Color',);
drawnow;
if ~strcmpi(get(fig,'Renderer'),'OpenGL')
errordlg('This demo requires OpenGL to look correct.');
end

[ 本帖最后由 chengweifeng 于 2007-9-3 19:56 编辑 ]

chengweifeng 发表于 2007-8-27 11:28:26

没人理!自己 顶一下!

这个程序包含:pushbuton,frame,text,
      实现多个radio button的单选,
      鼠标右键驱动(uicontextmenu)和菜单(uimenu)的实现.

function myguide2-2
% matlab 控件技术实现
% copyright
%cheng wf 2007 8 20 20:06
%
clear all
clc
figwin=figure('position',,...
    'name','Uicontrol:plot sin(x)',...
    'menubar','none',...
    'NumberTitle','off');
axeshandle=axes('position',,'box','on');
% create custom menu:zoom
itemzoom=uimenu(figwin,'label','&zoom');
itemzoomon=uimenu(itemzoom,'label','On');
itemzoomoff=uimenu(itemzoom,'label','Off');
set(itemzoomon,...
    'callback',['zoom on']);
set(itemzoomoff,...
    'callback',['zoom out']);
% create a frame
framebox=uicontrol(figwin,...
                   'style','frame',...
                   'position',);
% create three radio buttons.
RadioNum=3;
for i=1:RadioNum
    Radio_h(i)=0;
end
Radio_h(1)=uicontrol(figwin,...
         'style','radio',...
         'position',,...
         'String','Draw in Red',...
         'max',2,...
         'CallBack',...
         [ 'if get(gcbo,''value'')==1;',...
         'set(Radio_h(Radio_h~=gcbo),''value'',0);',...
         'end;',...
         'ColorStr=''r'';'...
         'set(get(gca,''children''),''Color'',''red'')']);
Radio_h(2)=uicontrol(figwin,'style','radio',...
         'position',,...
         'string','Draw in Green',...
         'max',2,...
         'CallBack',...
         ['if get(gcbo,''value'')==1;',...
         'set(Radio_h(Radio_h~=gcbo),''value'',0);',...
         'end;',...
         'ColorStr=''g'';'...
         'set(get(gca,''children''),''Color'',''green'')']);
Radio_h(3)=uicontrol(figwin,...
         'style','radio',...
         'position',,...
         'string','Draw in Blue',...
         'max',2,...
         'CallBack',...
         ['if get(gcbo,''value'')==1;',...
         'set(Radio_h(Radio_h~=gcbo),''value'',0);',...
         'end;',...
         'ColorStr=''b'';'...
         'set(get(gca,''children''),''Color'',''blue'')']);
% create a text
h1=uicontrol(figwin,...
             'style','text',...
             'string','函 数 曲 线 图:',...
             'position',,...
             'fontsize',16,...
             'backgroundcolor',get(gcf,'color'));
% create three buttons
push1=uicontrol(figwin,...
      'style','push',...
      'position',,...
      'string','Plot sin(x)',...
         'callback',...
         ['x=0:pi/20:2*pi;'...
         'Funhandle=plot(x,sin(x),ColorStr);'...
         'axis();'])
push2=uicontrol(figwin,...
      'style','push',...
      'position',,...
      'string','Cla',...
      'callback',['cla']);
push3=uicontrol(figwin,...
      'style','pushbutton',...
      'position',,...
      'string','Exit',...
      'callback',['close']);
% 建立鼠标右键所驱动的选项,句柄值返回rightmouse_point
rightmouse_point=uicontextmenu;
uimenu(rightmouse_point,'label','Width_2','callback','set(Funhandle,''linewidth'',2)');
uimenu(rightmouse_point,'label','Width_4','callback','set(Funhandle,''linewidth'',4)');
uimenu(rightmouse_point,'label','Width_6','callback','set(Funhandle,''linewidth'',6)');
uimenu(rightmouse_point,'label','Original','callback','set(Funhandle,''linewidth'',1)');
set(gca,'uicontextmenu',rightmouse_point)

ljq5987330 发表于 2007-8-27 14:21:21

正在学习,谢谢提供资料

zhaoluxing 发表于 2007-8-27 15:34:35

不错不错,很多东西相当有启发。尤其是创建右键的介绍,对于句柄的应用!

chengweifeng 发表于 2007-8-27 20:21:17

注意,对于radio button每次只能选取一个,在准备选取下一个之前,请首先取消先前选中的那个radio button.

kukusuiyi 发表于 2007-8-28 15:56:18

radio button 要实现单选功能貌似要用函数嵌套的方法才行
7.1版本下用一般书上或者网上的代码行不通,用函数嵌套可以实现
晓不得是不是我matlab版本的问题

bainhome 发表于 2007-8-28 20:31:56

楼主关于radio的描述可做出一定改进。
v7之前看看如下代码:
% radio按钮的布置
hPanelTypeCompute=uipanel(gcf,...
    'position',[.53,.13,.45,.17],...
    'titleposition','centertop',...
    'title','结构类型选择',...
    'fontweight','normal');
hRadioComputeType1=uicontrol(...
    'parent',hPanelTypeCompute,...
    'style','radio',...
    'tag','hRadioComputeType1',...
    'position',[.01,.6,.8,.28],...
    'string','框架结构');
hRadioComputeType2=uicontrol(...
    'parent',hPanelTypeCompute,...
    'style','radio',...
    'tag','hRadioComputeType2',...
    'position',[.01,.25,.8,.28],...
    'string','框架-剪力墙结构');

% radiobutton按钮的逻辑关系确定(初始化关系)
set(hRadioComputeType1,'value',get(hRadioComputeType1,'min'));
set(hRadioComputeType2,'value',get(hRadioComputeType2,'max'));% hRadioComputeType1按钮的回调函数
set(hRadioComputeType1,'callback',[...
    'global hFrameContent,',...
    'hRadioComputeType1=hFrameContent.hRadioComputeType1;',...
    'hRadioComputeType2=hFrameContent.hRadioComputeType2;',...
    'if get(hRadioComputeType1,''value'')~=0,',...
    'set(hRadioComputeType1,''value'',get(hRadioComputeType1,''max''));',...
    'set(hRadioComputeType2,''value'',get(hRadioComputeType2,''min''));',...
    'end']);

% hRadioComputeType2按钮的回调函数
set(hRadioComputeType2,'callback',[...
    'global hFrameContent,',...
    'hRadioComputeType1=hFrameContent.hRadioComputeType1;',...
    'hRadioComputeType2=hFrameContent.hRadioComputeType2;',...
    'if get(hRadioComputeType2,''value'')~=0,',...
    'set(hRadioComputeType1,''value'',get(hRadioComputeType1,''min''));',...
    'set(hRadioComputeType2,''value'',get(hRadioComputeType2,''max''));',...
    'end']);v7之后:
search uibuttongroup

[ 本帖最后由 bainhome 于 2007-8-28 20:33 编辑 ]

snow_man_0 发表于 2007-8-29 22:52:09

发代码前最好是说明下版本号

chengweifeng 发表于 2007-8-30 15:48:43

To kukusuiyi 兄:
 如果你有什么具体的方法可以这里分享一下!

To bainhome大侠:
 感谢斑竹指点迷津.其实,关于多个radio button的实现,我调试了一天也没调出来!当时用了如下三种方式,只有3#里的方法可以实现.高手可以一一更正.正准备问这个问题!!马上回去试一下大侠的代码!
1.见3#代码;
2.'set(Radio_h(1),''value'',1);',...  %未能实现想要的结果
  'set(Radio_h(2),''value'',0);',...
   'set(Radio_h(3),''value'',0);',...
      'ColorStr=''r'';',...
      'set(get(gca,''children''),''color'',''red'')']); 
3.['n=1;',...          %未能实现想要的结果
  'if get(Radio_h(1),''value'')==1;'...
      'set(Radio_h(),''value'',0);'...
      'else'...
      'set(Radio_h(1),''value'',1);'...
      'end;'...
      'ColorStr=''r'';'...
      'set(Funhandle,''Color'',''red'')'])
其实,我要的结果是,在我选择某个按纽时,其它按纽自动设置value=0.

To snow_man_0 大侠:
 谢谢提醒,"批评"的对!以后多多指教!

 我用的是:6.5.0.180913a (R13)

[ 本帖最后由 chengweifeng 于 2007-9-7 20:42 编辑 ]

ss502 发表于 2007-9-15 06:17:12

如果想让Radio的值输出到一个text里面,应该怎么做。

chengweifeng 发表于 2007-9-15 18:46:29

我不知道你说的radio的值是不是指它的属性value的值(0或1)?!
试试这个:
radio1=get(你的radio的句柄,‘value')
set(你的text的句柄,'string',num2str(radio1))

wkf-1983 发表于 2007-9-28 21:09:18

回复 #3 chengweifeng 的帖子

想问以下'Funhandle=plot(x,sin(x),ColorStr);'...中的ColorStr是怎么回事?

chengweifeng 发表于 2007-9-29 12:25:51

假设你点击的是Radio_h(1),则此处ColorStr=r;
sin(x)则为对应x的y值。
Funhandle为返回的所绘制图形的句柄值,以方便别处调用。
因此,此句就像你在书上经常见到的 :
h=plot(x,y,'r');
               
               版主不必理会我的回复,但是如有错误之处望指正

zhsewa1 发表于 2007-10-8 12:52:06

不错不错,很多东西相当有启发。

haixing123 发表于 2010-7-8 11:01:16

学习一下,刚开始学MATLAB

hesir 发表于 2010-8-13 17:05:21

把radio button放到Panel里面的话,默认就是只能选中一个的
页: [1]
查看完整版本: gui之基础篇(m-file)