MATLAB: Iterate through multiple serial ports, one after another (not simultaneously)

communicationMATLABserial

I have a program that utilizes incoming serial data to do a strcmp with data stored in excel. the string initially comes in looking like this:
Reader(n): 'String'
my code is set up to find the n, and then compare the serial string to the string stored in the Nth column of an excel spreadsheet. A bool value of 1 from this operation will change the Nth panel in a GUI to green, while a bool value of 0 will change the Nth panel to red.
At the moment, my code only iterates from n = 1:2. these are the n values coming in from COM3. I want to use 3 more COM ports, each one carrying n = 3:4, n = 5:6, and n = 7:8. However, I am unsure of how to initialize the ports to be read one after another, no simultaneously. this is my main function:
delete(instrfind('Port', 'COM3')); % removes possibility for 'Port not available' error
tag = serial('COM3'); %initializes the port to be used
fopen(tag); %opens th eport
BOX = char(zeros(2,14)); % matrix to be populated with incoming serial data
TrueValueData = 'C:\RfidChipTrueValues.xlsx';
% location of stored master tags
[~,~,TrueValMat] = xlsread(TrueValueData); % reads our excel file into the proper format
% 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:inf
for n = 1:2
if i>10 % positive reading
readData = fscanf(tag);
if length(readData)>12
BOX(str2double(readData(8)),1:14)= readData(11:24);
if strcmp(TrueValMat{2,n}, BOX(n,:)) %cannot sub-index to CELL types normally, must use this method
set(handles.uipanels(n), 'BackgroundColor', 'g');
else
set(handles.uipanels(n), 'BackgroundColor', 'r');
end
drawnow % allows GUI to update the appearance whenever the loop completes.
if strcmp(TrueValMat{2,1}, BOX(1,:))...
&& strcmp(TrueValMat{2,2}, BOX(2,:)) == 1
set(handles.pushbutton5, 'enable', 'on');
end
end
end
end
end
the GUI at this point works well, with only minor things to be ironed out in the future. the colors change correctly, etc.
This is all for only a single com port. how do I make this work with 3 additional ports?
I know about bytesavailablefcn, but I have no clues how to use it, even after hours spent reading about it, watching videos, etc. obviously I am open to using anything that works, but it will have to really be explained to me.
Thanks so much

Best Answer

portlist = {'COM3', 'COM4', 'COM7', 'COM12'};
nport = length(portlist);
tags = cell{1, nport};
cleanups = cell{1, nport};
for portidx = 1 : nport
delete(instrfind('Port', portlist{portidx})); % removes possibility for 'Port not available' error
tags{portidx} = serial(portlist{portidx}); %initializes the port to be used
fopen(tag); %opens th eport
cleanups{portidx} = onCleanup(@() fclose(portlist{portidx}));
end
BOX = char(zeros(2,14)); % matrix to be populated with incoming serial data
TrueValueData = 'C:\RfidChipTrueValues.xlsx';
% location of stored master tags
[~,~,TrueValMat] = xlsread(TrueValueData); % reads our excel file into the proper format
% 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:inf
for portidx = 1 : nport
nbase = portidx * 2 - 1;
for n = nbase:nbase+1
if i>10 % positive reading
readData = fscanf(tags{portidx});;
if length(readData)>12
BOX(str2double(readData(8)),1:14)= readData(11:24);
if strcmp(TrueValMat{2,n}, BOX(n,:)) %cannot sub-index to CELL types normally, must use this method
set(handles.uipanels(n), 'BackgroundColor', 'g');
else
set(handles.uipanels(n), 'BackgroundColor', 'r');
end
drawnow % allows GUI to update the appearance whenever the loop completes.
if strcmp(TrueValMat{2,1}, BOX(1,:))...
&& strcmp(TrueValMat{2,2}, BOX(2,:)) == 1
set(handles.pushbutton5, 'enable', 'on');
end
end
end
end
end
end
If you want something different to be done for the different ports, then you can test the portidx value.