feval error in GUI implementation

4 views (last 30 days)
I have this code that I have bult that compares a string coming over serial data to one that is stored in an excel file. A correct match makes a panel in the GUI go green, and red when they are not matched.
This is the code I have as of now:
function varargout = WorkingGUI2(varargin)
% WORKINGGUI2 MATLAB code for WorkingGUI2.fig
% WORKINGGUI2, by itself, creates a new WORKINGGUI2 or raises the existing
% singleton*.
%
% H = WORKINGGUI2 returns the handle to a new WORKINGGUI2 or the handle to
% the existing singleton*.
%
% WORKINGGUI2('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in WORKINGGUI2.M with the given input arguments.
%
% WORKINGGUI2('Property','Value',...) creates a new WORKINGGUI2 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before WorkingGUI2_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to WorkingGUI2_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help WorkingGUI2
% Last Modified by GUIDE v2.5 17-Sep-2019 15:09:28
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @WorkingGUI2_OpeningFcn, ...
'gui_OutputFcn', @WorkingGUI2_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before WorkingGUI2 is made visible.
function WorkingGUI2_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to WorkingGUI2 (see VARARGIN)
% Choose default command line output for WorkingGUI2
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes WorkingGUI2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = WorkingGUI2_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
%Begin Script
delete(instrfind('Port', 'COM3'));
tag = serial('COM3'); %check which port is used
fopen(tag);
BOX = char(zeros(2,14));
i=1;
c=0;
TrueValueData = 'C:\Mastercodes.xlsx';
[~,~,TrueValMat] = xlsread(TrueValueData); % Creates matrix filled with the correct values,
% indexed by box, which is the first row
% all proceeding rows are the master value
for i=1:9223372036854775807
if i>10 %first couple reads are filled with unicode nonsense, this skips that stage
readData = fscanf(tag);
if length(readData)>12
BOX(str2num(readData(8)),1:14)= readData(11:24); % these numbers just give us what we want;
% tags come in initially with some gobbledy-gook
end
if strcmp(TrueValMat{2,1}, BOX(1,:))
Reader1Correct;
else
Reader1Incorrect;
end
if strcmp(TrueValMat{2,2}, BOX(2,:))
Reader2Correct;
else
Reader2Incorrect;
end
if strcmp(TrueValMat{2,1}, BOX(1,:))...
&& strcmp(TrueValMat{2,2}, BOX(2,:)) == 1
break
end
end
end
% --- Executes during object creation, after setting all properties.
function uipanel1_CreateFcn(hObject, eventdata, handles1)
function Reader1Correct
set(handles1.uipanel1, 'BackgroundColor', 'g');
end
function Reader1Incorrect
set(handles1.uipanel1, 'BackgroundColor', 'r');
end
end
% hObject handle to uipanel1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% --- Executes during object creation, after setting all properties.
function uipanel2_CreateFcn(hObject, eventdata, handles2)
function Reader2Correct
set(handles2.uipanel2, 'BackgroundColor', 'g');
end
function Reader2Incorrect
set(handles2.uipanel2, 'BackgroundColor', 'r');
end
end
% hObject handle to uipanel2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
end
end
end
I don't know If have correctly made or called the functions in this script. I chnaged the handles monikor becasue I got a warning that it was used multiple times ( I don't know if that was necessary). When running the program, the GUI comes up jsut as I want it, but then get this error:
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)WorkingGUI2('uipanel1_CreateFcn',hObject,eventdata,guidata(hObject))
Undefined function or variable 'WorkingGUI2_OutputFcn'.
Error in gui_mainfcn (line 264)
feval(gui_State.gui_OutputFcn, gui_hFigure, [], gui_Handles);
Error in WorkingGUI2 (line 42)
gui_mainfcn(gui_State, varargin{:});
I don't think I need any arguments output from WorkingGUI2_Outputfcn, but I am not sure.
thanks for any help

Accepted Answer

Walter Roberson
Walter Roberson on 17 Sep 2019
You have
function uipanel1_CreateFcn(hObject, eventdata, handles1)
function Reader1Correct
set(handles1.uipanel1, 'BackgroundColor', 'g');
end
function Reader1Incorrect
set(handles1.uipanel1, 'BackgroundColor', 'r');
end
end
That defines nested functions inside upanel1_CreateFcn (but does not call those functions.) It is not possible to define nested functions inside code that uses "function" without the matching "end" statement, so you would have had to add "end" statements to a number of places in the code.
But when you did that, you did not pay attention to the indentation, and you mis-nested. The "end" statement to terminate WorkingGUI2_OpeningFcn is not until much later, so several functions ended up nesting inside WorkingGUI2_OpeningFcn -- including WorkingGUI2_Outputfcn, so the reference to it in the OpenFcn cannot find it because it is not visible from the scope of the OpenFcn
Nested functions do not play well with GUIDE. Once you insert any nested function, then even if you manage to put all the "end" in at the correct places yourself, GUIDE can no longer manage the modified .m file.
If you need nested functions, then you should probably put the enclosing function into a different .m file, and the defining function should not be the direct target of a callback, and should instead be called from a callback.
  11 Comments
Walter Roberson
Walter Roberson on 19 Sep 2019
If you have multiple com ports then I would recommend that you use BytesAvailableFcn callbacks on the serial ports instead of polling them.
It is certainly possible to do something like
p{1} = serial('COM2');
p{2} = serial('COM7');
for K = 1 : length(p)
buf{K} = fgetl(p{K});
end
to read lines from each port in turn. The difficulty with that is that if one of the ports has not sent anything, then this code will sit and wait around until that port gets around to sending a line -- and in the meantime all of the other ports might have sent multiple lines. BytesAvailableFcn callbacks on the other hand are run as soon as practical after MATLAB notices that a line is completely available from the serial port, so if one port sends lines (say) 4 times faster, then there is no problem.
avram alter
avram alter on 23 Sep 2019
ech one of the serial ports I will have will loop constantly at 9600 baud. I dont think i will have to wait for any particular serial port more than anything else. how would you implement your buffering code? I attmepted to replace my tag(serial) statements to what you wrote, But it didnt work. how would I use that?

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Identification in Help Center and File Exchange

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!