Error Using GUI after first time - "Error using handle.handle/set Invalid or deleted object."

I'm a beginner in guide and I'm doing something simple where I load (pushbutton) data and when I'm finished the first time it saves the number of files I loaded. Without closing the GUI I try to load again but I get this error message:
Error using handle.handle/set Invalid or deleted object.
Error in wrg_calc_01>loadbut_Callback (line 92) set(handles.nfiles,'String', str1)
Error in gui_mainfcn (line 96) feval(varargin{:});
Error in wrg_calc_01 (line 43) gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)wrg_calc_01('loadbut_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
------------\\-------------
The code where that this is referring to is the following:
% --- Executes on button press in loadbut.
function loadbut_Callback(hObject, eventdata, handles)
% hObject handle to loadbut (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%Select the files
[filesnames, pathname] = uigetfile({'*.wrg'},'Pick a file', 'MultiSelect', 'on');
if ischar(filesnames)==0
nfiles=length(filesnames);
str=char('Number of files selected: %d');
str1=sprintf(str,nfiles);
%sets the name presented in GUI as the path
set(handles.staload, 'String', pathname)
*set(handles.nfiles,'String', str1) %String that changes* whenever i change the number of files loaded
for i=length(filesnames):-1:1
data=fullfile(pathname, filesnames(i)); %complete path, cell type
data=char(data); %Change to char type
WRGMatrix(:,:,i)=WRGreader(data); %Function to read the files and save them in a 3D Matrix
end
handles.WRGMatrix=WRGMatrix;
handles.nfiles=nfiles;
guidata(hObject, handles);
elseif ischar(filesnames)==1
warndlg('Select atleast 2 files to compute')
end
-----------------------\\--------------------

 采纳的回答

This line:
handles.nfiles=nfiles;
will overwrite what I assume started off as a uicontrol the first time you run the code so that the 2nd time when you reach this line:
set(handles.nfiles,'String', str1)
handles.nfiles is no longer the uicontrol, it is instead what was set after the first run - i.e. just a number.
Never override a ui component on handles with just raw data. The component will still be there on the UI, but will no longer be accessible as it should be via the handles struct.

更多回答(1 个)

Well assumed!! It was that, I had the tag of a "ui static string" has nfiles and I was also using this name has a variable for the number of files I loaded.
THANKs!

类别

帮助中心File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by