将MATLAB GUI单选按钮设为数字可见性ON-OFF

时间:2016-07-19 08:38:27

标签: matlab

我正在为我的一个脚本开发MATLAB GUI。我的脚本有开始按钮,在其下我调用函数来解决方程式。我有3个数字。所以到目前为止我的GUI效果很好。发生的情况是,当用户在提供所需输入后单击“开始”时,会逐个弹出3个数字并指定保存数字的位置。循环很小时看起来不错,但是当循环很大时,计算时间会增加,因此弹出的数字会让用户感到恼火,因为用户无法做任何事情。

我想在GUI中引入一个单选按钮,取消选中该按钮将禁用数字的可见性。 我尝试了数字('可见','关')并且效果很好,但现在我希望它用单选按钮链接。

单选按钮回调是:

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

% Hint: get(hObject,'Value') returns toggle state of checkbox2

任何人都可以帮助我构建代码框架,使这个单选按钮在按钮上显示数字的可见性:

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

等待你的帮助。

2 个答案:

答案 0 :(得分:1)

Start_Callback检查复选框/ radiobutton的状态并创建一个新变量供以后使用:

if get(handles.checkbox2, 'Value')
    Visibility = 'on';
else 
    Visibility = 'off';
end

现在创建你的数字:

figure('Visible', Visibility)

取决于Visibility的值,它们是否可见

答案 1 :(得分:1)

我认为你的主要问题是设定所有三个数字的可见度 为了设置特定数字的可见性,最好的选择是保持对该数字的处理。

我建议在打开新图时保持图形处理:

h1 = figure; %Open new figure, and store figure handle in h1
h2 = figure; %Open new figure, and store figure handle in h2
h3 = figure; %Open new figure, and store figure handle in h3

我认为在GUI OpeningFcg函数中创建(打开)数字的正确位置。

使用指南工具创建新GUI时,Matlab会生成以下代码:

function guiname_OpeningFcg(hObject, evevntdata, handles, varargin)  
...
handles.output = hObject;

guidata(hObject, handles);
...

在代码行guidata(hObject, handles);之后,您可以创建数字 将图形句柄存储在handles结构中以供以后使用:

function guiname_OpeningFcg(hObject, evevntdata, handles, varargin) 
...
handles.output = hObject;

guidata(hObject, handles);

h1 = figure; %Open first figure, and store figure handle in h1
h2 = figure; %Open second figure, and store figure handle in h2
h3 = figure; %Open third figure, and store figure handle in h3

handles.h1 = h1; %Store handle to first figure.
handles.h2 = h2; %Store handle to second figure.
handles.h2 = h2; %Store handle to third figure.

在回调函数中,使用以下代码设置可见性:

% --- Executes on button press in checkbox2.
function checkbox2_Callback(hObject, eventdata, handles)

val = get(handles.checkbox2, 'Value');

if (val)
    vis = 'on';
else
    vis = 'off';
end

set(handles.h1, 'Visible', vis); %Set Visibility of first figure.
set(handles.h2, 'Visible', vis); %Set Visibility of second figure.
set(handles.h3, 'Visible', vis); %Set Visibility of third figure.