GUIDE GUI按钮的实现 [英] Implementation of GUIDE GUI buttons

查看:171
本文介绍了GUIDE GUI按钮的实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a question about the implementation of buttons inside the guide framework. I have 2 buttons created and a drop down menu not seen in the below code (referenced in NumC line). The way the program is meant to run is that they select something from the drop down menu, then the Generate button takes directly from the drop down menu. The third button, Optimize, needs the NumC variable and Cities matrix. Is it possible to reference them directly in the Optimize function, or do I first have to use some output feature in the Generate button to make those 2 usable. NumC is easy enough to recapture using the same line as in Generate, but I need the Cities matrix as it's generated.

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),'*')

推荐答案

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

You can use the guidata command to save data to the handles structure and make it available to the other function. Note how in the above code, the comment for handles reads structure with handles and user data (see GUIDATA). So you can do something like this

% --- 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.City .

In the body of your Optimize_Callback function, the NumC and Cities should be directly accessible from handles as handles.NumC and handles.Cities respectively.

这篇关于GUIDE GUI按钮的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆