管理多个回调
Manage multiple callbacks
我已经制作了一个用户界面,其中包含许多在循环中创建的 edit text 框,现在我想将用户输入存储在一个带有 callback[= 的变量中44=].
例如考虑这段代码,
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个可编辑框),
感谢您的帮助。
正如您在评论中提到的,您希望在按下 "Ok" 按钮后从编辑框中读取您的值。为此,您应该将回调函数分配给 "ok" 按钮,而不是编辑框:
uicontrol('Style', 'pushbutton', 'String', 'Ok',...
'Position', [100 5 60 30],'Callback', {@My_Callback});
在回调函数中,您应该从所有编辑框中读取值。
我建议您在定义编辑框时使用句柄(最好将其用于所有对象):
e1=uicontrol('style','edit','Position', [120 80 80 30],...
'BackgroundColor',bc,'ForegroundColor','w');
e2=uicontrol('style','edit','Position', [120 40 80 30],...
'BackgroundColor',bc,'ForegroundColor','w');
现在,为了读取值,您只需使用相应编辑框的句柄。
function My_Callback(hObject,eventdata)
p1 = str2double(get(e1,'string'));
p2 = str2double(get(e2,'string'));
end
注意:此句柄 (e1
,e2
) 在您的代码中将具有青色,这意味着它们是 "global" 变量,即用于主代码和回调函数。
如果您不想使用全局句柄,可以将它们传递给回调函数。所以你的回调函数会有更多的参数。
uicontrol('Style', 'pushbutton', 'String', 'Ok',...
'Position', [100 5 60 30],'Callback', {@My_Callback(e1,e2)});
function My_Callback(hObject,eventdata,e1,e2)
p1 = str2double(get(e1,'string'));
p2 = str2double(get(e2,'string'));
end
如果我没看错你的问题,你可以使用uicontrols
的UserData
属性来存储你输入的编辑框的内容,这样就可以了在您的 GUI 或其他地方的任何地方都可以很容易地恢复它们。
此外,为了方便起见,您可以在创建 uicontrol 时为其指定名称,因此当您需要获取其 属性 的 one/many 时,您可以使用其名称而不是 hObject
参数。
例如,假设您用 X 数据命名框 x1
,然后您可以创建它并将其存储在 GUI 的句柄结构中,如下所示:
handles.x1 = uicontrol(...)
所以当你需要获取一个属性时,你可以使用
get(handles.x1,'Some Property');
因此回到您的问题,您可以使用此语法设置 My_Callback
内所有框的 UserData
属性。之后你可以在任何你想要的回调中恢复它们。当然,一个简单的方法是获取编辑框的 String
属性 而不是它们的 UserData
,但是使用后者你可以存储任何你想要的可能会派上用场的东西。
在下面的 GUI 中,我修改了你的 GUI,为一个名为 DisplayData
的函数更改 'OK' 按钮的回调,该函数从每个编辑框获取 UserData
并显示它。
function p = myfun()
clear
clc
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');
%//===============
handles.x1 = uicontrol('style','edit's,'Position', [120 80 80 30],...
'BackgroundColor',bc,'ForegroundColor','w','Callback',{@My_Callback});
handles.y1 = 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', @(s,e) DisplayData);
movegui(f,'center')
set(f,'Visible','on')
guidata(f,handles); %// Updata guidata
function My_Callback(hObject,eventdata)
p = str2double(get(hObject,'string'));
%// Assign the content of the box to its "UserData" property
set(hObject,'UserData',p)
guidata(f,handles)
end
function DisplayData(~,~)
%// You could do it for all the boxes in your GUI.
x1Data = get(handles.x1,'UserData');
y1Data = get(handles.y1,'UserData');
fprintf('The number in box 1 is %0.2f and the number in box 2 is %0.2f\n',x1Data,y1Data);
guidata(f,handles)
end
end
示例输出:
并且按下"OK"按钮后,这个字符串显示在命令Window:
The number in box 1 is 2.00 and the number in box 2 is 4.00
希望对您有所帮助!
我已经制作了一个用户界面,其中包含许多在循环中创建的 edit text 框,现在我想将用户输入存储在一个带有 callback[= 的变量中44=].
例如考虑这段代码,
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个可编辑框),
感谢您的帮助。
正如您在评论中提到的,您希望在按下 "Ok" 按钮后从编辑框中读取您的值。为此,您应该将回调函数分配给 "ok" 按钮,而不是编辑框:
uicontrol('Style', 'pushbutton', 'String', 'Ok',...
'Position', [100 5 60 30],'Callback', {@My_Callback});
在回调函数中,您应该从所有编辑框中读取值。
我建议您在定义编辑框时使用句柄(最好将其用于所有对象):
e1=uicontrol('style','edit','Position', [120 80 80 30],...
'BackgroundColor',bc,'ForegroundColor','w');
e2=uicontrol('style','edit','Position', [120 40 80 30],...
'BackgroundColor',bc,'ForegroundColor','w');
现在,为了读取值,您只需使用相应编辑框的句柄。
function My_Callback(hObject,eventdata)
p1 = str2double(get(e1,'string'));
p2 = str2double(get(e2,'string'));
end
注意:此句柄 (e1
,e2
) 在您的代码中将具有青色,这意味着它们是 "global" 变量,即用于主代码和回调函数。
如果您不想使用全局句柄,可以将它们传递给回调函数。所以你的回调函数会有更多的参数。
uicontrol('Style', 'pushbutton', 'String', 'Ok',...
'Position', [100 5 60 30],'Callback', {@My_Callback(e1,e2)});
function My_Callback(hObject,eventdata,e1,e2)
p1 = str2double(get(e1,'string'));
p2 = str2double(get(e2,'string'));
end
如果我没看错你的问题,你可以使用uicontrols
的UserData
属性来存储你输入的编辑框的内容,这样就可以了在您的 GUI 或其他地方的任何地方都可以很容易地恢复它们。
此外,为了方便起见,您可以在创建 uicontrol 时为其指定名称,因此当您需要获取其 属性 的 one/many 时,您可以使用其名称而不是 hObject
参数。
例如,假设您用 X 数据命名框 x1
,然后您可以创建它并将其存储在 GUI 的句柄结构中,如下所示:
handles.x1 = uicontrol(...)
所以当你需要获取一个属性时,你可以使用
get(handles.x1,'Some Property');
因此回到您的问题,您可以使用此语法设置 My_Callback
内所有框的 UserData
属性。之后你可以在任何你想要的回调中恢复它们。当然,一个简单的方法是获取编辑框的 String
属性 而不是它们的 UserData
,但是使用后者你可以存储任何你想要的可能会派上用场的东西。
在下面的 GUI 中,我修改了你的 GUI,为一个名为 DisplayData
的函数更改 'OK' 按钮的回调,该函数从每个编辑框获取 UserData
并显示它。
function p = myfun()
clear
clc
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');
%//===============
handles.x1 = uicontrol('style','edit's,'Position', [120 80 80 30],...
'BackgroundColor',bc,'ForegroundColor','w','Callback',{@My_Callback});
handles.y1 = 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', @(s,e) DisplayData);
movegui(f,'center')
set(f,'Visible','on')
guidata(f,handles); %// Updata guidata
function My_Callback(hObject,eventdata)
p = str2double(get(hObject,'string'));
%// Assign the content of the box to its "UserData" property
set(hObject,'UserData',p)
guidata(f,handles)
end
function DisplayData(~,~)
%// You could do it for all the boxes in your GUI.
x1Data = get(handles.x1,'UserData');
y1Data = get(handles.y1,'UserData');
fprintf('The number in box 1 is %0.2f and the number in box 2 is %0.2f\n',x1Data,y1Data);
guidata(f,handles)
end
end
示例输出:
并且按下"OK"按钮后,这个字符串显示在命令Window:
The number in box 1 is 2.00 and the number in box 2 is 4.00
希望对您有所帮助!