GUI stops working after pushbutton function has completed

I have created a GUI (called GNgui) using GUIDE that runs a function when a 'Scan' button is pressed. It disables all the other buttons/fields in the GUI until it stops running, where it enables all the buttons again.
After the function runs (successfully), I'd like to be able to hit the 'Scan' button again to repeat the process, but every time I do, I get the following error message:
Undefined function 'GNgui' for input arguments of type 'struct'.
Error in
@(hObject,eventdata)GNgui('fileName_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
This isn't mutually exclusive to that push button either. When I try to press any other button (or even try to close the GUI window), I get a very similar error.
Any ideas? I've attached my .m file.
PS: I realise the code is a little messy, I'm trying to update and improve on some code someone else wrote.

댓글 수: 2

We'd need the .fig file also.
Apologies... I've attached the .fig file.

댓글을 달려면 로그인하십시오.

답변 (1개)

Meade
Meade 2016년 4월 20일
I believe that the call back is written incorrectly. Your anonymous function syntax is telling the callback function that the second input is "evt" which is a struct by default. However, you're passing "hObj" as the second input.
try this:
@(hObject,eventdata)GNgui(hObject,eventdata,guidata(hObject))

댓글 수: 7

Sorry, I'm new to GUI's. Which callback should I replace?
Remove 'fileName_Callback' and then put your debug stop inside the callback function (the little red dot to the left of the line).
What does it report?
Okay, so I removed the 'filename_callback' from the 'Filename' textbox callback function, and ran the code but the problem persists.
When I placed the debug stop in the 'fileName' function, it did not display anything and allowed me to 'continue'.
I don't think it's a problem with the fileName function though, since after hitting the 'Scan' button, and allowing it to complete its process, I can't then click on any of the other buttons (or even close the window). I got this error message when trying to run the scan again...
Undefined function or variable 'hObject'.
Error while evaluating uicontrol Callback
Undefined function 'GNgui' for input arguments of type 'struct'.
Error in
@(hObject,eventdata)GNgui('scanButton_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
Thanks for your help so far.
Can you post the code for your 'scan button'?
% --- Executes on button press in scanButton.
function scanButton_Callback(hObject, eventdata, handles)
% hObject handle to scanButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.scanButton, 'String', 'Scanning...', 'Enable', 'Inactive')
set(handles.cancelButton, 'Enable', 'On', 'UserData', 'START')
set(handles.saveLocation, 'Enable', 'Off')
set(handles.modeSelect, 'Enable', 'Off')
set(handles.sampleNo, 'Enable', 'Off')
set(handles.changeSamplesBetween, 'Enable', 'Off')
set(handles.KeepBlankData, 'Enable', 'Off')
set(handles.stWave, 'Enable', 'Off')
set(handles.enWave, 'Enable', 'Off')
set(handles.stepValue, 'Enable', 'Off')
set(handles.width_of_sample, 'Enable', 'Off')
global KeepBlank
NoSamples = str2double(get(handles.sampleNo,'String'));
StartWave = str2double(get(handles.stWave,'String'));
EndWave = str2double(get(handles.enWave,'String'));
StepSize = str2double(get(handles.stepValue,'String'));
Mode = get(handles.modeSelect,'Value');
flag = get(handles.changeSamplesBetween,'Value');
maxVal = get(handles.changeSamplesBetween,'Max');
KeepBlank = get(handles.KeepBlankData, 'String');
cancel = get(handles.cancelButton, 'UserData');
cTime = clock;
h2 = warndlg({['No of Repeats: ' num2str(NoSamples)];['Start Wavelength: ' num2str(StartWave)];['End Wavelength: ' num2str(EndWave)];['Step Size: ' num2str(StepSize)]},'Scan Settings');
uiwait(h2);
pauseVal = 0.01*StepSize;
global GN
%mode select
if Mode == 2
fprintf(GN,'CON');fscanf(GN);fscanf(GN); % select Concentration mode
Type = 'C';
else
fprintf(GN,'ABS');fscanf(GN);fscanf(GN); % select Absorbance mode
Type = 'ABS';
end
Wavelength = StartWave:StepSize:EndWave;
si = length(Wavelength);
% disp(KeepBlank)
%while strcmp(cancel, 'START')
if strcmp(KeepBlank, 'No')
%cancel = get(handles.cancelButton, 'UserData');
h3 = warndlg('Please Insert Blank, then click OK');
uiwait(h3);
% hw = waitbar(0,'Scanning Blank..');
blank = NaN(si,1);
for i = 1:si
fprintf(GN,['GTO ' num2str(Wavelength(i))]);pause(pauseVal);fscanf(GN);fscanf(GN);
if i == 1
pause(2); %give extra time to change WaveLength at start of scan
end
fprintf(GN,'SND');pause(pauseVal);fscanf(GN);
blank(i,1) = str2double(fscanf(GN));pause(pauseVal);fscanf(GN);
%waitbar(i/si);
axes(handles.axes5)
cla
rectangle('Position',[0,0,(round(1000*i/si))+1,20],'FaceColor','b');
text(480,10,['Scanning Blank...', num2str(round(100*i/si)),'%'], 'FontSize', 16);
pause(0.01)
axes(handles.axes1)
plot(Wavelength,blank,'-','linewidth',2);
title('Blank')
xlabel('Wavelength (nm)');ylabel(Type);
grid on
cancel = get(handles.cancelButton, 'UserData');
if strcmp(cancel, 'STOP')
error('Operation Aborted!')
end
end
%close(hw)
elseif strcmp(KeepBlank, 'Yes') && exist('blank', 'var') == 0
h3 = warndlg('Please Insert Blank, then click OK');
uiwait(h3);
%cancel = get(handles.cancelButton, 'UserData');
%hw = waitbar(0,'Scanning Blank..');
blank = NaN(si,1);
for i = 1:si
fprintf(GN,['GTO ' num2str(Wavelength(i))]);pause(pauseVal);fscanf(GN);fscanf(GN);
if i == 1
pause(2); %give extra time to change WaveLength at start of scan
end
fprintf(GN,'SND');pause(pauseVal);fscanf(GN);
blank(i,1) = str2double(fscanf(GN));pause(pauseVal);fscanf(GN);
%waitbar(i/si);
axes(handles.axes5)
cla
rectangle('Position',[0,0,(round(1000*i/si))+1,20],'FaceColor','b');
text(480,10,['Scanning Blank...', num2str(round(100*i/si)),'%'], 'FontSize', 16);
pause(0.01)
axes(handles.axes1)
plot(Wavelength,blank,'-','linewidth',2);
title('Blank')
xlabel('Wavelength (nm)');ylabel(Type);
grid on
cancel = get(handles.cancelButton, 'UserData');
if strcmp(cancel, 'STOP')
error('Operation Aborted!')
end
end
%close(hw)
end
sample = NaN(si,NoSamples);
VAL = NaN(si,NoSamples);
Abs = NaN(si,NoSamples);
%cancel = get(handles.cancelButton, 'UserData');
for j = 1:NoSamples
if j == 1
h4 = warndlg('Please Insert Sample, then press enter');
uiwait(h4)
elseif flag == maxVal
h4 = warndlg('Please Insert Next Sample, then press enter');
uiwait(h4)
% else
end
%hw = waitbar(0,['Scanning Sample number ' num2str(j) ' out of ' num2str(NoSamples) '.']);
for i = 1:si
fprintf(GN,['GTO ' num2str(Wavelength(i))]);pause(pauseVal);fscanf(GN);fscanf(GN);
if i == 1
pause(2); %give extra time to change WaveLength at start of scan
end
fprintf(GN,'SND');pause(pauseVal);fscanf(GN);
sample(i,j) = str2double(fscanf(GN));pause(pauseVal);fscanf(GN);
%waitbar(i/si);
VAL(i,j) = sample(i,j) - blank(i,1);
axes(handles.axes5)
cla
rectangle('Position',[0,0,(round(1000*i/si))+1,20],'FaceColor','b');
text(480,10,['Scanning Sample Number ', num2str(j), '/', num2str(NoSamples), '...', num2str(round(100*i/si)),'%'], 'FontSize', 14);
%title(['Scanning Sample Number ', num2str(j), '/', num2str(NoSamples)])
pause(0.01)
% figure(2);
% plot(Wavelength,VAL,'.','linewidth',2);
% xlabel('Wavelength (nm)');ylabel(Type);
axes(handles.axes2)
plot(Wavelength,sample,'-r','linewidth',2);
title('Sample')
xlabel('Wavelength (nm)');ylabel(Type);
grid on
axes(handles.axes3)
plot(Wavelength,VAL,'-','linewidth',2);
title(Type)
xlabel('Wavelength (nm)');ylabel(Type);
grid on
if strcmp(Type, 'ABS')
ww=str2double(get(handles.width_of_sample,'String'));
Abs(i,j)=VAL(i,j)/ww;
axes(handles.axes4)
plot(Wavelength,Abs,'-r','linewidth',2);
title('Absorption Coefficent')
xlabel('Wavelength (nm)');ylabel('Absorpation coefficent(mm^1)');
grid on
cancel = get(handles.cancelButton, 'UserData');
if strcmp(cancel, 'STOP')
error('Operation Aborted!')
end
end
end
%close(hw)
end
% blankgvbblankblankcvblankvblankblankblankblankblank
% save file
name = get(handles.fileName,'String');
DIR = get(handles.saveLocation, 'UserData');
filename = [name, '_', date];
cd(DIR);
save([filename, '.mat'],'Wavelength','VAL','NoSamples','sample','blank','Abs');
set(handles.scanButton, 'String', 'Scan', 'Enable', 'On')
set(handles.cancelButton, 'Enable', 'Off', 'UserData', 'STOP')
set(handles.saveLocation, 'Enable', 'On')
set(handles.modeSelect, 'Enable', 'On')
set(handles.sampleNo, 'Enable', 'On')
set(handles.changeSamplesBetween, 'Enable', 'On')
set(handles.KeepBlankData, 'Enable', 'On')
set(handles.stWave, 'Enable', 'On')
set(handles.enWave, 'Enable', 'On')
set(handles.stepValue, 'Enable', 'On')
set(handles.width_of_sample, 'Enable', 'On')
%end
if strcmp(KeepBlank, 'Yes')
assignin('base', 'blank', blank)
elseif strcmp(KeepBlank, 'No')
clear blank
end
guidata(hObject, handles);
end
Ok, so the problem seems to be that the 'handles' argument that is being passed to ALL your callbacks is empty.
I can't run the full gui since it's looking for some sort of COM connection, but you need to get all the handles for all the things you created in the GUIDE creator.
As a start, swap lines 41:45 with these
hFig = ancestor(hObject,'figure');
handles = guihandles(hFig);
% Choose default command line output for GNgui
handles.output = hObject;
% Update handles structure
guidata(hFig, handles);
This adds the structure 'handles' as guidata to the figure. Then, whenever a callback has the 'handles' arument, it should get this list of items.
I updated lines 41:45 with the code you provided but I still receive the following error:
Undefined function 'GNgui' for input arguments of type 'struct'.
Error in
@(hObject,eventdata)GNgui('fileName_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
Undefined function 'GNgui' for input arguments of type 'struct'.
Error in
@(hObject,eventdata)GNgui('scanButton_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
When you say I need to 'get all the handles', what should I do with them? Again, sorry about being a bit of a novice when it comes to these things.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

제품

질문:

2016년 4월 20일

댓글:

2016년 4월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by