MATLAB: Pushbutton in GUIDE gives the error “Undefined function or variable ‘Test_wone_file_1′” only if other callback are previously activated

callbackguidehandles

Here I am again in my series of questions to get from A to Z on my project. Here is my previous questions. I finally understood how to pass the handles between different callbacks. I want now to plot the curves of the selected checkboxes when I press the pushbutton "Plot!".
If If I press it as first thing nothing happens (the callback is still empty, see code below).
If I press it when I actually want to plot, I get the error: (notice that Test-wone-file_1 is my Guide).
Undefined function or variable 'Test_wone_file_1'.
Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)Test_wone_file_1('PB2_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
Do you have any Idea of what could it be? I attach as always the files and the file .TRA if someone wants to try the code.
% --- Executes on button press in PB1.
function PB1_Callback(hObject, eventdata, handles)
dname = uigetdir('C:\Users\MDl\Dropbox\epfl\Internship\matlab scripts');
if dname == 0
return
else
cd(dname)
end
F = dir('*.TRA');
for i = 1:length(F)
names{i,1} = F(i,1).name;
end
% Create cell 'false' of same length of names
checkboxes = num2cell(false(length(names),1));
% Create the two columns for uitable
myData =[checkboxes names];
% Set the uitable
columneditable = [true false];
columnformat = {'logical', 'char'};
set(handles.uitable2,'data', myData,...
'ColumnFormat', columnformat,...
'ColumnEditable', columneditable,...
'CellEditCallback',@fctCB);
handles.myData = myData;
handles.files = cell(length(F),2);
guidata(hObject, handles)
end
function fctCB(hObject,eventdata)
handles = guidata(hObject);
files = handles.files;
myData = handles.myData;
if eventdata.NewData == 1
% read the file only if it isn't already there
if isempty(files{eventdata.Indices(1),1})
fName = myData(eventdata.Indices(1),2);
fileName = fopen(fName{1,1}, 'r');
C = textscan(fileName, '%s %s %s', 'Delimiter', ';');
% create two subcells in files at the index eventdata containing the force and displacement
for i = 1:length(C{1,1})-17
files{eventdata.Indices(1),1}{i,1} = str2double(C{1,1}{i+17});
files{eventdata.Indices(1),2}{i,1} = str2double(C{1,2}{i+17});
end
end
else
end
% f1 = cell2mat(files{eventdata.Indices(1),1});
% f2 = cell2mat(files{eventdata.Indices(1),2});
% hold all
% plot(f1,f2)
handles.files(eventdata.Indices(1),:) = files(eventdata.Indices(1),:);
guidata(hObject, handles);
end
% --- Executes on button press in PB2.
function PB2_Callback(hObject, eventdata, handles)
handles = guidata(hObject);
% files = handles.files;
% CB_answer = cell2mat(handles.uitable2.Data(1:end,1));
% cla;
% set(handles.axes1, 'Visible', 'off')
% for i = 1:length(CB_answer)
% if CB_answer(i) == 1
% f1 = cell2mat(files{i,1});
% f2 = cell2mat(files{i,2});
% hold all
% plot(f1,f2)
% else
% end
% end
% set(handles.axes1, 'Visible', 'on')
guidata(hObject, handles);
end

Best Answer

Look at comment :)
So, to help you get started with editing your code to handle TRA file in other directories, you need to edit PB1_CallBack to store the directory name, dname, somewhere and then use that when loading your data in fctCB.
Edit PB1_Callback like this:
function PB1_Callback(hObject, eventdata, handles)
dname = uigetdir('C:\Users\MDl\Dropbox\epfl\Internship\matlab scripts');
if dname == 0
return
end
F = dir(fullfile(dname, '*.TRA'));
names = {F.name}'; %<USE THIS instead of looping %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Create cell 'false' of same length of names
checkboxes = num2cell(false(length(names),1));
% Create the two columns for uitable
myData =[checkboxes names];
% Set the uitable
columneditable = [true false];
columnformat = {'logical', 'char'};
set(handles.uitable2,'data', myData,...
'ColumnFormat', columnformat,...
'ColumnEditable', columneditable,...
'CellEditCallback',@fctCB);
handles.myData = myData;
handles.filepath = dname; %<ADD THIS%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
handles.files = cell(length(F),2);
guidata(hObject, handles)
end
Edit fctCB like this:
function fctCB(hObject,eventdata)
handles = guidata(hObject);
files = handles.files;
myData = handles.myData;
if eventdata.NewData == 1
% read the file only if it isn't already there
if isempty(files{eventdata.Indices(1),1})
fName = myData(eventdata.Indices(1),2);
fileName = fopen(fullfile(handles.filepath, fName{1,1}), 'r'); %<EDIT THIS%%%%%%%%%%%%%%%%
C = textscan(fileName, '%s %s %s', 'Delimiter', ';');
% create two subcells in files at the index eventdata containing the force and displacement
for i = 1:length(C{1,1})-17
files{eventdata.Indices(1),1}{i,1} = str2double(C{1,1}{i+17});
files{eventdata.Indices(1),2}{i,1} = str2double(C{1,2}{i+17});
end
end
else
end
% f1 = cell2mat(files{eventdata.Indices(1),1});
% f2 = cell2mat(files{eventdata.Indices(1),2});
% hold all
% plot(f1,f2)
handles.files(eventdata.Indices(1),:) = files(eventdata.Indices(1),:);
guidata(hObject, handles);
end