Pushbutton in GUIDE gives the error "Undefined function or variable 'Test_wone_file_1'" only if other callback are previously activated

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

6 commentaires

I can't get the the error you're describing. In fact, it's working when you get rid of the "%" of the commented codes in PB2_Callback function.
When I click the checkbox, and then push Plot!, it plots just those that are selected. Is this an unwanted behavior? Also, you should edit the opening function to have handles.files = {}; initialization to prevent error if pressing the Plot! without selecting a directory.
% --- Executes just before Test_wone_file_1 is made visible.
function Test_wone_file_1_OpeningFcn(hObject, eventdata, handles, varargin)
% Choose default command line output for Test_wone_file_1
handles.output = hObject;
handles.files = {}; %<============HERE!!!!
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Test_wone_file_1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
end
It does work for you? I still get an error. Might it be the version of matlab? unluckily I am using an old R2012b. Now that I have added the handle.files = {} command I get this error when I press plot:
Undefined function 'Test_wone_file_1' for input arguments of type
'struct'.
Error in
@(hObject,eventdata)Test_wone_file_1('PB2_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
How is it possible that the same code works for you and not for me :'(
Now I have choosen a different folder with only one file .TRA and I get this error...
Attempt to reference field of non-structure array.
Error in Test_wone_file_1>PB2_Callback (line 107)
CB_answer = cell2mat(handles.uitable2.Data(1:end,1));
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in Test_wone_file_1 (line 18)
gui_mainfcn(gui_State, varargin{:});
Error in
@(hObject,eventdata)Test_wone_file_1('PB2_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
Actually no matter what I push after I have clicked the PB1 (except for the checkboxes) and I get the same error.
Undefined function 'Test_wone_file_1' for input arguments of type 'struct'.
How are you running the GUI? Do NOT double-click the .fig file, as this causes an error - it will skip the initialization functions, causing a lot of issues. Run the gui by doing this:
>> Test_wone_file_1
I click on the execute command (F5). I will try as you say and come back to this.
This is what happens, I downloaded the zip folder here attached and it works... The exact same file...
Could it be because I execute the file saved on Dropbox? I just don't understand xD
I have found the problem, really stupid. I was opening the .TRA files from another folder than the one were my matlab file is saved. Hence I need now to understand how to tell the program to go back to that folder. thanks for your help. If you want to answer to this question properly (with the button answer, you can also write: "look to the comments") I will approve it.

Connectez-vous pour commenter.

 Réponse acceptée

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

Plus de réponses (0)

Catégories

En savoir plus sur Entering Commands dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by