在Matlab GUI中创建0-100%的对象

时间:2015-01-25 21:22:30

标签: matlab user-interface

我目前正在为我的Matlab程序创建一个GUI(图形用户界面),在其中一个程序中,其中一个参数是整体的百分比。 我想要做的是,创建一些形式的对象,呈现0-100的数字,用户每按一次箭头按钮,将数字增加1或减少-1。 是否有这样的对象可以帮助我做到这一点?我该如何创建呢?

1 个答案:

答案 0 :(得分:0)

这就是你要找的东西:

function test_perc

figh = figure();
% create a text control that will display the 0 - 100 text
texth = uicontrol('Style', 'text', 'Units', 'normalized', ...
    'Position', [0.1, 0.1, 0.8, 0.8], 'FontSize', 56, ...
    'String', '0');
% set a function that handles key presses (uses handle to the text object)
set(figh, 'WindowKeyPressFcn', @(hobj, ev) percfun(ev, texth));

function percfun(ev, texth)
% check if leftarrow or rightarrow has been 
% pressed and modify text accordingly 
if strcmp(ev.Key, 'leftarrow')
    val = max(0, str2num(get(texth, 'String')) - 1);
    set(texth, 'String', num2str(val));
elseif strcmp(ev.Key, 'rightarrow')
    val = min(100, str2num(get(texth, 'String')) + 1);
    set(texth, 'String', num2str(val));
end

上面的代码创建了figureuicontrol样式'text'。然后将该图设置为响应具有特定功能(WindowKeyPressFcn)的按键(percfun),该功能通过设置uicontrol的文本来响应箭头。 如果您有任何问题 - 请问,我会澄清。