一个matlab_GUI可编辑框、弹出框、列
框、按键示例的问题剖析
matlab_GUI % 可编辑框、弹出框、列表框、按键示例
% 目标:制作一个能绘制任意图形的交互界面。它包括:可编辑文本框、弹出
框、列表框。
% 这个例子是我在一本matlab教程
上看到得一个示例,非常好的一个例子。 % 可怎么运行都出不来结果,弄了半天发现原来是字符串没有转换成数字表达
式的原因。(蓝色部分为后加)
% 加上以后发现时可以运行出结果的。原来以为7.0和6.5都行,用7.0版本高,功能多还向下兼容,
%原来却不是这 样。7.0的确向下兼容,但这里兼容的意思只是不报错,而至
于能不能运行出在6.5
%中运行出的结果来,那就不得而知了。大家可以拿去学习学习哈~
clf reset % <1>
set(gcf, 'unit' , 'normalized' , 'position' ,[0.1,0.4,0.85,0.35]); % 设置图形窗大小 set(gcf, 'defaultuicontrolunits' , 'normalized' );
set(gcf, 'defaultuicontrolfontsize' ,11);
set(gcf, 'defaultuicontrolfontname' , ' 隶书 ' );
set(gcf, 'defaultuicontrolhorizontal' , 'left' );
set(gcf, 'menubar' , 'none' ); % 删除图形窗工具条
str= ' 通过多行指令绘图的交互界面 ' ;
set(gcf, 'name' ,str, 'numbertitle' , 'off' ); % 书写图形窗名 h_axes=axes( 'position' ,[0.05,0.15,0.45,0.70], 'visible' , 'off' ); % 定义轴位框位置
uicontrol(gcf, 'Style' , 'text' , ... % 制作静态文本框
'position' ,[0.52,0.87,0.26,0.1], ...
'String' , ' 绘图指令输入框 ' );
hedit=uicontrol(gcf, 'Style' , 'edit' , ... % 制作可编辑文本框 <14> 'position' ,[0.52,0.05,0.26,0.8], ...
'Max' ,2); % 取 2 ,使 Max-Min>1 ,而允许多行输入 <16>
hpop=uicontrol(gcf, 'style' , 'popup' , ... % 制作弹出菜单 <17>
'position' ,[0.8,0.73,0.18,0.12], ...
'string' , '张老师|summer|autumn|winter' ); % 设置弹出框中选项名 <19> hlist=uicontrol(gcf, 'Style' , 'list' , ... % 制作列表框 <20>
'position' ,[0.8,0.23,0.18,0.37], ...
'string' , '生气Grid on|打人Box on|摔手机Hidden off|吵架Axis off' , ... % 设置列表框中选项名 <22>
'Max' ,2); % 取 2 ,使 Max-Min>1 ,而允许多项选择 <23>
hpush=uicontrol(gcf, 'Style' , 'push' , ... % 制作与列表框配用的按键 <24> 'position' ,[0.8,0.05,0.18,0.15], 'string' , '应用Apply' );
set(hedit, 'callback' , 'calledit(hedit,hpop,hlist)' ); % 编辑框输入引起回调 <26> set(hpop, 'callback' , 'calledit(hedit,hpop,hlist)' ); % 弹出框选择引起回调 <27> set(hpush, 'callback' , 'calledit(hedit,hpop,hlist)' ); % 按键引起的回调 <28>
%将下面的code去comment,然后剪切,另存为calledit.m函数 % function calledit(hedit,hpop,hlist)
% ct=str2num(get(hedit, 'string' )); % 获得输入的字符串函数 <2> % vpop=get(hpop, 'value' ); % 获得选项的位置标识 <3>
% vlist=get(hlist, 'value' ); % 获得选项位置向量 <4>
% if ~isempty(ct) % 可编辑框输入非空时 <5>
% eval('ct') % 运行从编辑文本框送入的指令 <6>
% popstr={ 'spring' , 'summer' , 'autumn' , 'winter' }; % 弹出框色图矩阵 <7> % liststr={ 'grid on' , 'box on' , 'hidden off' , 'axis off' }; % 列表框选项内容 <8> % invstr={ 'grid off' , 'box off' , 'hidden on' , 'axis on' }; % 列表框的逆指令 <9> % colormap(eval(popstr{vpop})) % 采用弹出框所选色图 <10>
% vv=zeros(1,4);vv(vlist)=1;
% for k=1:4
% if vv(k);eval(liststr{k}); else eval(invstr{k}); end % 按列表选项影响图形 % end
% end