2016-03-14 36 views
0

我寫一個代碼,MATLAB GUI的圖像對的比較,但我堅持,因爲我的單選按鈕沒有對exclusivley工作單選按鈕不僅限於工作,在MATLAB給錯誤GUI

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我選擇一個單選按鈕出現以下錯誤

「沒有足夠的輸入參數。

錯誤而評估uicontrol回調。」

錯誤是在設置ieset(handles.radio2,'Value',0)的行上;

我需要在這方面幫助...

回答

1

首先,如果你希望你的單選按鈕一起操作(只能選擇一個),想要把它們放入一個uibuttongroup

其次,你的錯誤的原因是因爲標準 MATLAB回調(與uicontrol(..., 'Callback', @callback, ...)中定義的)只有通過輸入參數(與3 GUIDE圖形用戶界面)。

這些輸入是:

  1. 生成回調(手柄到您的按鈕)
  2. 任何關聯的事件數據

所以,你將要修改myRadio接受兩個對象輸入參數。

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 

的另一種選擇是手動指定使用anonymous function的輸入您的回調函數。你可以用它來創建一個帶有三個輸入的回調(就像GUIDE自己的回調),然後你的myRadio函數可以保持原樣。

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

所有這一切不談,如果你使用了uibuttongroup,因爲它是所有可確保只有一個單選按鈕,在選擇時,你不會需要此回調。

+0

謝謝......我在使用按鈕使用switch語句時遇到了一些錯誤,但解決了單選按鈕工作的問題......謝謝 – farhan