Problem Error when trying to appear / disappear a text edit based on some radio buttons within a button group

I'm making a GUI where basically I have 3 radio button within a button group and out I have 3 edit text. Depending on the selection of a radio edit button disappears for a particular text, entry in the other 2 values and operate with them. Up there in the first run and all good, the problem is that after that first calculate the change in radio button and get rid of a text edit and re-make it appear that he was invisible and will not let me get the following error: ??? Error using ==> set Invalid handle object.
Error in ==> Direct_E>uipanel1_SelectionChangeFcn at 102
set(handles.p1,'Visible','off');
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> Direct_E at 44
gui_mainfcn(gui_State, varargin{:});
Error in ==>
@(hObject,eventdata)Direct_E('uipanel1_SelectionChangeFcn',get(hObject,'SelectedObject'),eventdata,guidata(get(hObject,'SelectedObject')))
Error in ==> hgfeval at 63
feval(fcn{1},varargin{:},fcn{2:end});
Error in ==> uitools.uibuttongroup.childAddedCbk>manageButtons at 80
hgfeval(cbk, source, evdata);
??? Error while evaluating uicontrol Callback
This is my source code:
function varargout = Direct_E(varargin)
% DIRECT_E M-file for Direct_E.fig
% DIRECT_E, by itself, creates a new DIRECT_E or raises the existing
% singleton*.
%
% H = DIRECT_E returns the handle to a new DIRECT_E or the handle to
% the existing singleton*.
%
% DIRECT_E('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in DIRECT_E.M with the given input arguments.
%
% DIRECT_E('Property','Value',...) creates a new DIRECT_E or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Direct_E_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Direct_E_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help Direct_E
% Last Modified by GUIDE v2.5 29-May-2011 18:54:04
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Direct_E_OpeningFcn, ...
'gui_OutputFcn', @Direct_E_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 Direct_E is made visible.
function Direct_E_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 Direct_E (see VARARGIN)
% Choose default command line output for Direct_E
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Direct_E wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Direct_E_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)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes when selected object is changed in uipanel1.
function uipanel1_SelectionChangeFcn(hObject, eventdata, handles)
global seleccion
switch get(eventdata.NewValue,'Tag')
case 'boton_a'
set(handles.a,'Visible','off');
set(handles.text1,'Visible','off');
set(handles.b1,'Visible','on');
set(handles.text2,'Visible','on');
set(handles.p1,'Visible','on');
set(handles.text3,'Visible','on');
seleccion=1;
case 'boton_b1'
set(handles.b1,'Visible','off');
set(handles.text2,'Visible','off');
set(handles.a,'Visible','on');
set(handles.text1,'Visible','on');
set(handles.p1,'Visible','on');
set(handles.text3,'Visible','on');
seleccion=2;
case 'boton_p1'
set(handles.p1,'Visible','off');
set(handles.text3,'Visible','off');
set(handles.b1,'Visible','on');
set(handles.text2,'Visible','on');
set(handles.a,'Visible','on');
set(handles.text1,'Visible','on');
seleccion=3;
end
guidata(hObject,handles);
function a_Callback(hObject, eventdata, handles)
NewStrVal=get(hObject,'String');
NewVal = str2double(NewStrVal);
handles.a=NewVal;
guidata(hObject,handles);
% --- Executes during object creation, after setting all properties.
function a_CreateFcn(hObject, eventdata, handles)
% hObject handle to a (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function b1_Callback(hObject, eventdata, handles)
NewStrVal=get(hObject,'String');
NewVal = str2double(NewStrVal);
handles.b1=NewVal;
guidata(hObject,handles);
% --- Executes during object creation, after setting all properties.
function b1_CreateFcn(hObject, eventdata, handles)
% hObject handle to b1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function p1_Callback(hObject, eventdata, handles)
NewStrVal=get(hObject,'String');
NewVal = str2double(NewStrVal);
handles.p1=NewVal;
guidata(hObject,handles);
% --- Executes during object creation, after setting all properties.
function p1_CreateFcn(hObject, eventdata, handles)
% hObject handle to p1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in REPRESENTAR.
function REPRESENTAR_Callback(hObject, eventdata, handles)
global seleccion
if seleccion == 1
p1=handles.p1;
b1=handles.b1;
mx = handles.mx;
a = linspace(0,mx,1000);
termino_Fresnel = b1./(sqrt(2*p1));
C = mfun('FresnelC',termino_Fresnel);
S = mfun('FresnelS',termino_Fresnel);
directividad = ((64.*a.*p1)./(pi.*b1)).*(C.^2+S.^2);
axes(handles.axes1);
handles.x=a;
handles.h=plot(a,directividad);
elseif seleccion == 2
p1=handles.p1;
mx=handles.mx;
a=handles.a;
b1 = linspace(0,mx,1000);
termino_Fresnel = b1./(sqrt(2*p1));
C = mfun('FresnelC',termino_Fresnel);
S = mfun('FresnelS',termino_Fresnel);
directividad = ((64.*a.*p1)./(pi.*b1)).*(C.^2+S.^2);
axes(handles.axes1);
handles.x=b1;
handles.h=plot(b1,directividad);
elseif seleccion == 3
mx=handles.mx;
p1=linspace(0,mx,1000);
a=handles.a;
b1 = handles.b1;
termino_Fresnel = b1./(sqrt(2*p1));
C = mfun('FresnelC',termino_Fresnel);
S = mfun('FresnelS',termino_Fresnel);
directividad = ((64.*a.*p1)./(pi.*b1)).*(C.^2+S.^2);
axes(handles.axes1);
handles.x=p1;
handles.h=plot(p1,directividad);
end
guidata(hObject,handles);
function mx_Callback(hObject, eventdata, handles)
NewStrVal=get(hObject,'String');
NewVal = str2double(NewStrVal);
handles.mx=NewVal;
guidata(hObject,handles);
% --- Executes during object creation, after setting all properties.
function mx_CreateFcn(hObject, eventdata, handles)
% hObject handle to mx (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit5_Callback(hObject, eventdata, handles)
% hObject handle to edit5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit5 as text
% str2double(get(hObject,'String')) returns contents of edit5 as a double
% --- Executes during object creation, after setting all properties.
function edit5_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
Does anyone know why this happens? Thank you very much, Luis

Réponses (5)

It's a bit difficult to see what's going on without the .fig file created by GUIDE. However, I've put together a basic function which shows the behaviour you're trying to get, I think.
Hopefully the code is easy enough to understand and will give you an idea why yours is failing.
Andy
% START CODE
function test_ToggleControls
% Create figure
h = figure('Visible','on');
% Add two radio buttons
uicontrol('Parent',h, ...
'Callback',@cb_radioBtn1, ...
'Position',[50 100 100 18], ...
'String','Button 1', ...
'Style','RadioButton', ...
'Tag','radioBtn1');
uicontrol('Parent',h, ...
'Callback',@cb_radioBtn2, ...
'Position',[50 80 100 18], ...
'String','Button 2', ...
'Style','RadioButton', ...
'Tag','radioBtn2');
% Add two edit boxes
uicontrol('Parent',h, ...
'Position',[155 100 100 18], ...
'Style','Edit', ...
'Tag','edit1', ...
'Visible','off');
uicontrol('Parent',h, ...
'Position',[155 80 100 18], ...
'Style','Edit', ...
'Tag','edit2', ...
'Visible','off');
% Callback functions for radio button 1
function cb_radioBtn1(h, eventData)
handles = guihandles(gcbf);
set([handles.radioBtn1 handles.radioBtn2],'Value',0)
set(h,'Value',1)
set([handles.edit1 handles.edit2],'Visible','off')
set(handles.edit1,'Visible','on')
% Callback functions for radio button 2
function cb_radioBtn2(h, eventData)
handles = guihandles(gcbf);
set([handles.radioBtn1 handles.radioBtn2],'Value',0)
set(h,'Value',1)
set([handles.edit1 handles.edit2],'Visible','off')
set(handles.edit2,'Visible','on')
% END CODE
Thanks for your answer.The problem is when the program does his first calculation on function "REPRESENTAR_Callback", I don´t understand why it is happens. I have reviewed it 100 times but I don´t understand. Help me please. Thank you.

1 commentaire

Luis,
In REPRESENTAR_Callback you seem to using uicontrol handle values within your calculations. This may not be the cause of your problem, but it seems strange.
For example: if seleccion = 1, you use the uicontrol handles b1 and p1 to calculate termino_Fresnel
I haven't managed to replicate your error. Can you post the .fig file that goes with your code somewhere?
Andy

Connectez-vous pour commenter.

What is your e-mail? My email is: luismanuelfernandezortiz@hotmail.com. Please, send me a message and I can send you my source code and my figure. Thank you.
I have upload my .fig and .m files. This is the link:
Thank you,
Luis

1 commentaire

I'm afraid I can't do anything with your code as it causes a segmentation violation in MATLAB. I'm only using R2006b and I assume your code was created in a later version. In particular, the form of the uicontrol callbacks has changed.
Sorry about that.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Graphics Object Properties dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by