MATLAB: Error while evaluating uicontrol Callback when using two buttons

guimatlab gui

Hi everyone, I have got a Problem with my GUI. My program has two buttons, I use the first button to choose a specific file in subfolder.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

[filename, pathname, filterindex] = uigetfile( ...
{ '*.mat','MAT-files (*.mat)'; ...
'*.slx;*.mdl','Models (*.slx, *.mdl)'; ...
'*.txt','Text Files (*.txt)'; ...
'*.*', 'All Files (*.*)'}, ...
'Pick a file', ...
'MultiSelect', 'on',...
'..\Schaltungen\');
if isequal(filename,0)
disp('User selected: Cancel')
else
disp(['User selected ', fullfile(pathname, filename)])
end
global fname;
assignin ('base','fname',filename);
%assignin ('base','path',pathname);
%fname = strcat(pathname,filename);
%assignin ('base','fname',fname);
%callback of button 1
handles.fname = fname;
guidata(hObject, handles)
When I declare the button 2 with containing the m.file , the code in m.file is
[Name, N1, N2, arg3]=textread(fname,'%s %s %s %s ');
in which to process variable fname which i have got already at button 1. And the code for callback in Button 2 is
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global fname;
fname = 'handles.fname';
And my program got some errors,
Error using textread (line 165)
File not found.
Error in scam (line 13)
[Name, N1, N2, arg3]=textread(fname,'%s %s %s %s ');
Error in demogui>pushbutton2_Callback (line 112)
scam
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in demogui (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)demogui('pushbutton2_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
I don't know exactly what happening with this program. Hope to receive your supports.

Best Answer

Tan - I think that the error may be originating in the second push button callback where you assign
fname = 'handles.fname';
and so the file that textread will attempt to read from is the string 'handles.filename' and not the filename that is referenced by this field. Wrapping this variable in quotes simply creates a string of that variable name.
You also don't need to declare fname as a global or assign it into the base workspace. Like you have already shown in the first pushbutton callback, just assign it as a field to the handles structure. The callback can then be simplified as
function pushbutton1_Callback(hObject, eventdata, handles)
[filename, pathname, filterindex] = uigetfile( ...
{ '*.mat','MAT-files (*.mat)'; ...
'*.slx;*.mdl','Models (*.slx, *.mdl)'; ...
'*.txt','Text Files (*.txt)'; ...
'*.*', 'All Files (*.*)'}, ...
'Pick a file', ...
'MultiSelect', 'on',...
'..\Schaltungen\');
if isequal(filename,0)
disp('User selected: Cancel')
handles.fname = '';
else
fname = fullfile(pathname, filename);
disp(['User selected ', fname]);
handles.fname = fname;
end
guidata(hObject, handles)
In the above, we save the path and filename to handles.fname (if the doesn't choose a file, then this field is set to an empty string). Now, in your second callback you can access this file as
function pushbutton2_Callback(hObject, eventdata, handles)
if isfield(handles,'fname')
if ~isempty(handles.fname)
fname = handles.fname;
[Name, N1, N2, arg3]=textread(fname,'%s %s %s %s ');
end
end