GUIDE GUI按钮的实现

时间:2014-05-30 04:11:17

标签: matlab

我对指南框架内按钮的实现有疑问。我创建了2个按钮,并在下面的代码中看不到下拉菜单(在NumC行中引用)。程序运行的方式是从下拉菜单中选择一些内容,然后直接从下拉菜单中选择Generate按钮。第三个按钮Optimize需要NumC变量和Cities矩阵。是否可以直接在Optimize函数中引用它们,或者我是否必须在Generate按钮中使用某些输出功能才能使这些功能可用。 NumC很容易使用与Generate中相同的行重新捕获,但我需要生成Cities矩阵。

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

% --- Executes on button press in Generate.
function Generate_Callback(hObject, eventdata, handles)
% hObject    handle to Generate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
NumC = get(handles.NumCities, 'Value');
NumC = NumC*10;
Cities = rand(NumC,2);
cla                                         %clears current window
plot(Cities(1:NumC,1),Cities(1:NumC,2),'r')
hold on
plot(Cities(1:NumC,1),Cities(1:NumC,2),'*')

1 个答案:

答案 0 :(得分:1)

您可以使用 guidata 命令将数据保存到句柄结构,并使其可用于其他功能。请注意,在上面的代码中,句柄的注释读取带有句柄和用户数据的结构(参见GUIDATA)。所以你可以做这样的事情

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

% etc. everything that you have above

% now save the data
handles.NumC   = NumC;
handles.Cities = NumC;
guidata(hObject,handles);

Optimize_Callback 功能的正文中, NumC 城市应该可以从句柄直接访问分别为 handles.NumC handles.Cities