单选按钮不能独占工作并在MATLAB GUI中给出错误 [英] Radio Button not working exclusively and giving error in MATLAB GUI

查看:118
本文介绍了单选按钮不能独占工作并在MATLAB GUI中给出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为MATLAB GUI编写代码以进行图像的成对比较,但由于我的单选按钮无法正常工作,我被卡住了

I am writing a code for MATLAB GUI for pair comparison of images but i am stuck because my radio button are not working exclusivley

gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @GUI_Personality_Impressions_OpeningFcn, ...
                   'gui_OutputFcn',  @GUI_Personality_Impressions_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before GUI_Personality_Impressions is made visible.
function GUI_Personality_Impressions_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to GUI_Personality_Impressions (see VARARGIN)

% Choose default command line output for GUI_Personality_Impressions
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes GUI_Personality_Impressions wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = GUI_Personality_Impressions_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.axes1,'units','pixels');
set(handles.axes2,'units','pixels');
scrz=get(0,'ScreenSize')
% pos2=[(scrz(3)-800)/2  (scrz(4)-600)/2 800 600];
fig_hr = 326;
fig_vr = 493;


pos1 = round((scrz(3)-fig_hr)/4)
pos2 = round((scrz(4)-fig_vr)/2)

% fig_xcoord = (ScreenSize(3) - fig_width)/2; 

pos3 = [pos1 pos2 fig_hr fig_vr]
set(handles.axes1,'pos',[pos3]);
axes(handles.axes1);  
imshow('Chinese_eyes+2.tif');
% pos1 = round((scrz(3)-fig_hr)/  3)
posa = pos1 +1.5* round(fig_hr);
pos4 = [posa pos2 fig_hr fig_vr]
set(handles.axes2,'pos',[pos4]);
axes(handles.axes2);
imshow('Chinese_eyes+2.tif');
% % Get default command line output from handles structure
varargout{1} = handles.output;


% handles.FigureH = figure;
handles.radio1 = uicontrol('Style', 'radiobutton', ...
                           'Callback', @myRadio, ...
                           'Units',    'pixels', ...
                           'Tag',      'A1', ...
                           'Position', [(pos1+326+pos1)/2, pos2-70,70 ,50 ], ...
                           'String',   'A', ...                           
                           'Value',    1);
handles.radio2 = uicontrol('Style', 'radiobutton', ...
                           'Callback', @myRadio, ...
                           'Units',    'pixels', ...
                           'Position', [(posa+326+posa)/2, pos2-70,70 ,50], ...
                           'String',   'B', ...
                           'Tag',      'B1', ...
                           'Value',    0);

%  handles.Next= uicontrol('Style', 'pushbutton', ...
%                            'Callback', @pushbutton1, ...                           
%                            'Units',    'pixels', ...
%                            'Position', [(((pos1+326+pos1)/2)+(posa+326+posa)/2)/2, pos2- 140,70 ,50 ], ...
%                            'String',   'Next', ...
%                            'Value',    0);                    
% set(handles.Next,'Enable','off')                       

guidata(hObject, handles);



function myRadio(hObject,eventdata, handles)
global data

switch get(hObject,'Tag') % Get Tag of selected object.
    case 'A1'
       data=1;
        set(handles.radio2, 'Value', 0);
    case 'B1'
       data=2;    
      set(handles.radio1, 'Value',0);


end

并且当在GUI上我选择一个单选按钮时,出现错误

and when on the GUI I select a radio button following errors appear

输入参数不足.

评估uicontrol回调时出错."

Error while evaluating uicontrol Callback."

错误出现在set即设置(handles.radio2,'Value',0);的行上

The error is on the line for set i.e.set(handles.radio2, 'Value', 0);

在这方面我需要帮助...

I need help in this regard...

推荐答案

首先,如果希望单选按钮一起操作(只能选择一个按钮),则希望将它们放入 uibuttongroup .

First of all, if you want your radio buttons to operate together (only be able to select one), you want to put them into a uibuttongroup.

第二,出现错误的原因是因为标准 MATLAB回调(使用uicontrol(..., 'Callback', @callback, ...)定义的回调)仅传递了两个输入参数(对于GUIDE,则为3) GUI).

Second, the reason for your error is because the standard MATLAB callbacks (the ones defined with uicontrol(..., 'Callback', @callback, ...)) only pass two input arguments (compared to 3 for GUIDE GUIs).

这些输入是:

  1. 生成回调的对象(按钮的句柄)
  2. 任何关联的事件数据

因此,您将需要修改myRadio以接受两个输入参数.

So you will want to modify myRadio to accept two input arguments.

function myRadio(hObject, eventData)

    switch get(hObject,'Tag') % Get Tag of selected object.
        case 'A1'
            set(handles.radio2, 'Value', 0);
        case 'B1'
            set(handles.radio1, 'Value',0);
    end
end

另一个选择是使用匿名功能.您可以使用它来创建具有三个输入的回调(就像GUIDE自己的回调一样),然后您的myRadio函数可以保持不变.

The other option is to manually designate the inputs to your callback function using an anonymous function. You could use this to create a callback with three inputs (just like GUIDE's own callbacks) and then your myRadio function could remain as you have it.

set(handles.radio1, 'Callback', @(src,evnt)myRadio(src, evnt, handles))

除此之外,如果您使用uibuttongroup,则不会需要该回调,因为它所做的是确保每次仅选择一个单选按钮.

All that aside, if you use a uibuttongroup, you won't need this callback since all it does is ensures that only one radio button is selected at a time.

这篇关于单选按钮不能独占工作并在MATLAB GUI中给出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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