I wrote a code that compares a code coming in from a serial reader to a known set of codes. This is the code:
delete(instrfind('Port', 'COM3'));tag = serial('COM3'); %check which port is used
fopen(tag);%readData = fscanf(tag); %initialize
%writeData = uint16(500); % 0x01F4
%fwrite(tag, writeData, 'uint16') %write data
BOX = char(zeros(2,14)); i=1;c=0;TrueValueData = 'C:\Location_of_known_codes.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 values
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(length(readData)>10) %if chip has been read
%
% ReadChips
if strcmp(TrueValMat{2,1}, BOX(1,:)) disp('hurray1')%set(handles.uipanel3, 'Backgroundcolor', 'g');
else disp('not hurray1') %set(handles.uipanel3, 'Backgroundcolor', 'r');
end if strcmp(TrueValMat{2,2}, BOX(2,:)) disp('hurray2') %set(handles.uipanel2, 'Backgroundcolor', 'g');
else disp('not hurray2') %set(handles.uipanel2, 'Backgroundcolor', 'r');
end end end
This code works as I want it to, much of the comments are leftovers from the code I initially found online. I want to turn this into a GUI, where instead of printing out 'hurray1' or 'not hurray1', it changes the color of a button in the GUI (as seen in the comments on line 31-41). Note that the above code does exactly what I want it to.
To this end, I used app designer, and played around until i came up with this code:
classdef GuiScanner_1 < matlab.apps.AppBase % Properties that correspond to app components
properties (Access = public) UIFigure matlab.ui.Figure GUITESTLabel matlab.ui.control.Label READER1Button matlab.ui.control.StateButton READER2Button matlab.ui.control.StateButton enddelete(instrfindall); clearclctag = serial('COM3'); %check which port is usedfopen(tag);%readData = fscanf(tag); %initialize%writeData = uint16(500); % 0x01F4%fwrite(tag, writeData, 'uint16') %write dataBOX = char(zeros(2,14)); i=1;c=0;TrueValueData = 'C:\Location_of_known_codes.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 valuesfor 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 methods (Access = private) % Value changed function: READER1Button
function READER1ButtonValueChanged(app, event) value = app.READER1Button.Value; if strcmp(TrueValMat{2,1}, BOX(1,:)) app.READER1Button.BackgroundColor = [0 1 0]; else app.READER1Button.BackgroundColor = [1 0 0]; end end % Value changed function: READER2Button
function READER2ButtonValueChanged(app, event) value = app.READER2Button.Value; if strcmp(TrueValMat{2,2}, BOX(2,:)) app.READER2Button.BackgroundColor = [0 1 0]; else app.READER2Button.BackgroundColor = [1 0 0]; end end end % App initialization and construction
methods (Access = private) % Create UIFigure and components
function createComponents(app) % Create UIFigure
app.UIFigure = uifigure; app.UIFigure.Position = [100 100 640 480]; app.UIFigure.Name = 'UI Figure'; % Create GUITESTLabel
app.GUITESTLabel = uilabel(app.UIFigure); app.GUITESTLabel.Position = [305 234 59 15]; app.GUITESTLabel.Text = 'GUI TEST'; % Create READER1Button
app.READER1Button = uibutton(app.UIFigure, 'state'); app.READER1Button.ValueChangedFcn = createCallbackFcn(app, @READER1ButtonValueChanged, true); app.READER1Button.Text = 'READER 1'; app.READER1Button.BackgroundColor = [1 1 0]; app.READER1Button.Position = [285 439 100 22]; % Create READER2Button
app.READER2Button = uibutton(app.UIFigure, 'state'); app.READER2Button.ValueChangedFcn = createCallbackFcn(app, @READER2ButtonValueChanged, true); app.READER2Button.Text = 'READER 2'; app.READER2Button.BackgroundColor = [1 1 0]; app.READER2Button.Position = [285 24 100 22]; end end methods (Access = public) % Construct app
function app = GuiScanner_1 % Create and configure components
createComponents(app) % Register the app with App Designer
registerApp(app, app.UIFigure) if nargout == 0 clear app end end % Code that executes before app deletion
function delete(app) % Delete UIFigure when app is deleted
delete(app.UIFigure) end endend
I have no idea if I even placed any of my code in the correct places at all. I keep getting an 'Unexpected MATLAB code' error on line 11, and I can't find what that means.
Another issue is that I want the color of the buttons to stay yellow, until something has actually been scanned, and not immediately turn red. I assume that would need a loop to keep the color yellow when BOX is still a matrix of only zeros. Again, I dont know whereor how to put that into th eGUI code.
Any help is much appreciated.
Best Answer