Matlab GUI - 如何调用同一个GUI界面

3

我正在尝试使用Matlab编写一个GUI,让用户输入点作为输入和它们之间的连接。

我有5个Matlab文件 - screen1.m,screen2.m,screen3.m,screen4.m,globalParams.m

在globalParams中,我有全局参数,所以我可以从屏幕GUI到屏幕GUI使用它们。在screen1中,用户输入节点的数量(例如5)。当他按下“下一个”按钮时,回调函数调用“screen2();”。在screen2.m中,用户输入(x,y)坐标,当他按下“下一个”按钮时,回调函数调用“screen3();”。

现在我要求他填写节点i到节点j之间的连接。(他需要填写节点i和j的数字)。如果只有一个连接,他将按下“完成”按钮,回调函数将调用“screen4();”,一切都很好。否则(有多个连接),他按下“下一个”按钮,回调函数调用“screen3();”。 因此,当我们有多个连接时,我在再次调用screen3时遇到问题。

还有一种方法,当我调用下一个屏幕时关闭上一个屏幕吗? 因为当我们找到一种方法来一遍又一遍地调用screen3时,将会有大量的GUI打开,这可能会让用户感到困惑和烦恼。

一些代码:

在screen1中,下一个按钮:

function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
screen2();

在screen2中,下一个按钮:

function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

screen3();

在屏幕3中,点击下一步按钮,然后是完成按钮。
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
screen3();

% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
screen4();

在screen3中,我是如何使用2个节点之间的连接的:
function edit2_Callback(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit2 as text
%        str2double(get(hObject,'String')) returns contents of edit2 as a double

global hopsMatrix;
i = str2num(get(handles.edit2, 'string'));
j = str2num(get(handles.edit1, 'string'));
hopsMatrix(i,j) = 1;

为什么再次调用 screen3() 会有问题?是出现了错误,还是无法正常工作? - scenia
它只是不再调用screen3了.. 它只是去做一件事 - Kobi Guetta
你在 edit2 回调函数中评估数据是否有特定的原因?如果没有,请查看我的答案以获得更健壮的方法。 - scenia
1个回答

1
我不会再调用screen3()。你只需要清除编辑字段,显示成功消息并让他再次操作即可。
将数据评估(目前在edit2_Callback中的部分)移动到“下一步”按钮,然后在获取数据后进行处理。
set(handles.edit1, 'String', '');
set(handles.edit2, 'String', '');
set(handles.text1, 'String', sprintf('Connection (%d, %d) was added.',i,j));

请不要忘记在某个地方添加一个静态文本字段来显示消息(它应该自动接收句柄text1)。
这样,用户可以添加任意数量的节点,单击“下一步”以清除字段并添加另一个连接,或单击“完成”继续。
edit2回调中没有必要立即将连接添加到您的数据中(这也会带来一些问题,例如,如果用户首先输入第二点或在已经输入第二个编辑字段的情况下注意到第一点中的错误)。
关于删除,每个GUI都有一个指向其父图形的句柄,即handles.figure1,您可以在调用下一个GUI之前直接关闭它。因此,不要只写screen2();,而是写成:
close(handles.figure1);
screen2();

你给我的第一个解决方案很好用。但是第二个方案,使用close函数会导致错误。错误在screen1>pushbutton1_Callback (line 147)中引用了不存在的字段'figure'。 - Kobi Guetta

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接