在我看来,使用 GUI 时最好的选择是使用 GUI 的句柄结构,其中除了(这是很酷的部分)您想要存储的任何内容之外,每个 uicontrol 及其相关属性都存储在其中在其中,例如变量。
所以我修改了您的代码以使用句柄结构。我不完全清楚您想要什么,但在我的示例中,按钮用于使用第一个编辑框的内容更新第二个编辑框的内容。这是非常基本的,但它应该可以帮助您了解句柄和句柄结构。如果有什么不清楚的地方请告诉我!
function gui_function()
ScreenSize = get(0,'ScreenSize');
handles.figure = figure('Position',[ScreenSize(3)/2,ScreenSize(4)/2,400,285]);
handles.Edit1 = uicontrol('style', 'edit','Position',[100 150 75 50], ...
'string', '5');
handles.Edit2 = uicontrol('style', 'edit','Position',[100 80 75 50], ...
'string', 'Update me');
handles.computeButton = uicontrol('style', 'push','Position',[200 100 75 75],'String','PushMe', ...
'callback', @PushButtonCallback);
guidata(handles.figure, handles); %// Save handles to guidata. Then it's accessible form everywhere in the GUI.
function PushButtonCallback(handles,~)
handles=guidata(gcf); %// Retrieve handles associated with figure.
TextInBox1 = get(handles.Edit1,'String');
set(handles.Edit2,'String',TextInBox1); %// Update 2nd edit box with content of the first.
%// Do whatever you want...
guidata(handles.figure, handles); %// DON'T forget to update the handles structure
您可以通过添加函数回调 (test_script) 来自定义此 GUI,方法与我实现 PushButtonCallback 的方式相同。希望我明白你想要什么:)