Change the value into table in GUI

1 view (last 30 days)
isra sahli
isra sahli on 10 Dec 2021
Answered: _ on 10 Dec 2021
I have this GUI , where I enter the values, plot the fibers and save the results in a file,
The program does successfully, but when I try to change any value in the table and press the (Plot the Fibers) button, the GUI does not work and this message appears to me
(Error using handle.handle/get
Invalid or deleted object.
Error in illcol_setup>plotthefibers_Callback (line 121)
beam_radius=str2num(get(handles.beamradius,'string'));
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in illcol_setup (line 17)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)illcol_setup('plotthefibers_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
)
How can I solve the problem?
the code is:
function varargout = illcol_setup(varargin)
% ILLCOL_SETUP MATLAB code for illcol_setup.fig
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @illcol_setup_OpeningFcn, ...
'gui_OutputFcn', @illcol_setup_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
% --- Executes just before illcol_setup is made visible.
function illcol_setup_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
% --- Outputs from this function are returned to the command line.
function varargout = illcol_setup_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
% --- Executes on button press in collimated.
function collimated_Callback(hObject, eventdata, handles)
% hObject handle to collimated (see GCBO)
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in diffuse.
function diffuse_Callback(hObject, eventdata, handles)
% hObject handle to diffuse (see GCBO)
% handles structure with handles and user data (see GUIDATA)
function beamradius_Callback(hObject, eventdata, handles)
% hObject handle to beamradius (see GCBO)
% handles structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.
function beamradius_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in uniform.
function uniform_Callback(hObject, eventdata, handles)
% hObject handle to uniform (see GCBO)
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in gaussian.
function gaussian_Callback(hObject, eventdata, handles)
% hObject handle to gaussian (see GCBO)
% handles structure with handles and user data (see GUIDATA)
function incident_angle_Callback(hObject, eventdata, handles)
% hObject handle to incident_angle (see GCBO)
% handles structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.
function incident_angle_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function numerical_aperture_Callback(hObject, eventdata, handles)
% hObject handle to numerical_aperture (see GCBO)
% handles structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.
function numerical_aperture_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function the_radius_of_center_null_area_Callback(hObject, eventdata, handles)
% hObject handle to the_radius_of_center_null_area (see GCBO)
% handles structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.
function the_radius_of_center_null_area_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function refractive_index_Callback(hObject, eventdata, handles)
% hObject handle to refractive_index (see GCBO)
% handles structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.
function refractive_index_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function numberoffibers_Callback(hObject, eventdata, handles)
% hObject handle to numberoffibers (see GCBO)
% handles structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.
function numberoffibers_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in plotthefibers.
function plotthefibers_Callback(hObject, eventdata, handles)
% hObject handle to plotthefibers (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
cla;
beam_radius=str2num(get(handles.beamradius,'string'));
pai=0:pi/100:2*pi;
a=beam_radius*cos(pai);
b=beam_radius*sin(pai);
axes(handles.axes1)
plot (a,b,'b');
xlim ([-5000 5000]);
ylim ([-5000 5000]);
%this part is to draw the collecting fibers
number_of_collecting_fibers=str2num(get(handles.numberoffibers,'string')) % the number of collecting fibers
if isempty(number_of_collecting_fibers)
set (handles.warrning,'String','Warrning: Enter the number of collecting fibers.');
else
Dataofcollectingfibers=get (handles.collectingfibersdatatable, 'data') %the data of the table
set (handles.warrning,'String','Warrning: Enter the data of collecting fibers.');
hold on;
for i=1:number_of_collecting_fibers
for j=1:8
if isempty(Dataofcollectingfibers{i,j})
Data_of_collecting_fibers(i,j) = NaN
else
Data_of_collecting_fibers(i,j) = str2num(Dataofcollectingfibers{i,j})
end
end
end
for n=1:number_of_collecting_fibers
condition=Data_of_collecting_fibers (n,8)
if (condition==0) % this condition to choose the shape of the fiber that circle or rectangle
% the (0) mean that the shape of the fiber is circle and (1) mean that the shape of the fiber is rectangle
a=Data_of_collecting_fibers (n,1)
b=Data_of_collecting_fibers (n,2)
x=Data_of_collecting_fibers(n,3)
y=Data_of_collecting_fibers(n,4)
plot(a*cos(pai)+x,b*sin(pai)+y,'g');
else
width=Data_of_collecting_fibers (n,1);
high=Data_of_collecting_fibers (n,2);
xCentre=Data_of_collecting_fibers (n,3);
yCentre=Data_of_collecting_fibers (n,4);
x1=xCentre-(width/2);
x2=xCentre+(width/2);
y1=yCentre-(high/2);
y2=yCentre+(high/2);
x=[x1 x1 x2 x2 x1];
y=[y1 y2 y2 y1 y1];
plot(x,y,'g');
end
end
handles.beamradius=beam_radius;
handles.number_of_collecting_fibers=number_of_collecting_fibers;
handles.Data_of_collecting_fibers=Data_of_collecting_fibers;
end
guidata(hObject, handles);
% --- Executes on button press in acceptandsave.
function acceptandsave_Callback(hObject, eventdata, handles)
% hObject handle to acceptandsave (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
HWaitBar = waitbar(0, 'saving in process');
H1= uigetdir(path)
H2='\'
H3=sprintf('illcol_setup.txt');
H=[H1 H2 H3]; % Name of illcol_setup.txt files
fout= fopen(H,'wt');
%this part is to acceptandsave the parameters of illumination fiber
fprintf(fout,'Illumination Parameters\n');
beamtype=handles.beamtype;
if (beamtype=='Collimated')
beamtype=0;
else beamtype=1;
end
fprintf(fout,'%d\t\t\t\t#Beam type:0-Collimated,1-Diffuse\n', beamtype);
the_radius_of_center_null_area=str2num(get(handles.the_radius_of_center_null_area,'string'));
beam_radius=handles.beamradius./1000000; %convert from micrometer to cm.
fprintf(fout,'%5.4f\t%5.4f\t\t\t#Beam radius(cm) The radius of center null area(cm)\n',beam_radius,the_radius_of_center_null_area);
beamradial=handles.beamradial;
if (beamradial=='Uniform')
beamradial=0;
else beamradial=1;
end
fprintf(fout,'%5.4f\t%5.4f\t\t\t#Beam radial and angular profile: 0-uniform, 1-Gaussian\n',0, beamradial);
incident_angle=str2num(get(handles.incident_angle,'string'));
fprintf(fout,'%d\t\t\t\t#Incident angle (degree, w.r.t. the normal axis)\n', incident_angle);
refractive_index=str2num(get(handles.refractive_index,'string'));
fprintf(fout,'%d\t\t\t\t#Refractive index of illumination fiber\n', refractive_index);
numerical_aperture=str2num(get(handles.numerical_aperture,'string'));
fprintf(fout,'%d\t\t\t\t#Numerical Aperture of Illumination Fiber\n\n\n\n', numerical_aperture);
% this part of code is to get the parameters of collecting fibers
fprintf(fout,'Collection Parameters\n');
fprintf(fout,'%d\t\t#Employ collecting fibers\n',1);
fprintf(fout, '%d\t\t#The number of collecting fibers\n',handles.number_of_collecting_fibers);
fprintf(fout,'#Fiber-Radius/width(cm) Fiber-High(cm) CC_Dist_x(cm) CC_Dist_y(cm) Refractive-Index Numerical-Aperture Tilt-Angle shape-fiber(0 if Circle 1 if rectangle)\n');
Data_of_collecting_fibers=handles.Data_of_collecting_fibers;
for ii = 1:handles.number_of_collecting_fibers
for jj=1:4
fprintf(fout,'%g\t\t\t',Data_of_collecting_fibers(ii,jj)./1000000);
end
for jjj=5:8
fprintf(fout,'%g\t\t\t',Data_of_collecting_fibers(ii,jjj));
end
fprintf(fout,'\n');
end
guidata(hObject, handles);
for i = 1:100
waitbar(i / 100);
end
delete(HWaitBar);
% --- Executes when selected object is changed in beamradial.
function beamradial_SelectionChangedFcn(hObject, eventdata, handles)
beamradial=get(handles.beamradial,'selectedObject');
handles.beamradial=beamradial;
guidata(hObject, handles);
% --- Executes when selected object is changed in beamtype.
function beamtype_SelectionChangedFcn(hObject, eventdata, handles)
beamtype=get(handles.beamtype,'selectedObject');
handles.beamtype=beamtype;
guidata(hObject, handles);
function warrning_Callback(hObject, eventdata, handles)
% hObject handle to warrning (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 warrning as text
% str2double(get(hObject,'String')) returns contents of warrning as a double
% --- Executes during object creation, after setting all properties.
function warrning_CreateFcn(hObject, eventdata, handles)
% hObject handle to warrning (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

Answers (1)

_
_ on 10 Dec 2021
handles.beamradius is an Edit box in your GUI, but in the function plotthefibers_Callback, you have this line toward the end:
handles.beamradius=beam_radius;
which overwrites the value previously stored as handles.beamradius. That is, after plotthefibers_Callback executes once, then handles.beamradius no longer refers to the Edit box and instead refers to a number. So the next time when you do get(handles.beamradius,'string') you get that error because handles.beamradius is a number not a uicontrol. So maybe change that line to something like:
handles.beam_radius=beam_radius;
so that handles.beamradius is still the Edit box and handles.beam_radius is the number.

Tags

Community Treasure Hunt

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

Start Hunting!