【发布时间】:2015-05-04 13:53:22
【问题描述】:
我已经制作了一个用户界面,其中包含许多在循环中创建的编辑文本框,现在我想将用户输入存储在一个带有回调的变量中。
例如考虑这段代码,
function p = myfun()
f = figure;
set(f,'Position',[200 350 250 150],'Color',[.4 .6 .4],'MenuBar','none',...
'Visible','off');
bc = [.4 .6 .4];
uicontrol('Style','text','Position',[50 80 80 30],...
'String','X','BackgroundColor',bc,'ForegroundColor','w');
uicontrol('Style','text','Position',[50 40 80 30],...
'String','Y','BackgroundColor',bc,'ForegroundColor','w');
uicontrol('style','edit','Position', [120 80 80 30],...
'BackgroundColor',bc,'ForegroundColor','w','Callback',{@My_Callback});
uicontrol('style','edit','Position', [120 40 80 30],...
'BackgroundColor',bc,'ForegroundColor','w','Callback',{@My_Callback});
uicontrol('Style', 'pushbutton', 'String', 'Ok',...
'Position', [100 5 60 30],'Callback', 'close');
movegui(f,'center')
set(f,'Visible','on')
function My_Callback(hObject,eventdata)
p = str2double(get(hObject,'string'));
end
end
现在My_Callback 将被调用两次,但只有最后一个会存储在p 中。
但我希望它们像 p.x 和 p.y 一样存储。
我想我应该使用Tag,它说:
Tag
string (GUIDE sets this property)
User-specified object label. The Tag property provides a means to identify graphics objects with a user-specified label. This is particularly useful when constructing interactive graphics programs that would otherwise need to define object handles as global variables or pass them as arguments between callback routines. You can define Tag as any string.
但我不知道如何(我有大约 16 个可编辑框),
感谢您的帮助。
【问题讨论】:
-
您希望何时从编辑框中读取值,在输入后立即读取,还是在按下“确定”按钮后读取?
-
@MikhailGenkin,在按下
ok之后,因为用户可能会更改它们。谢谢
标签: matlab function user-interface