MATLAB Answers

convert xlsread code to use a cell array

5 views (last 30 days)
avram alter
avram alter on 30 Dec 2019
Commented: Walter Roberson on 31 Dec 2019
I have a code that compares the cells of an excel sheet to incoming data over serial. The incoming string looks like this:
Reader1: 0071803F6AA40%
the code takes the number after the reader, and uses that to tell which row the proper tag is located (1 is A, 2 is B, etc). it then compares the tag, and if they are the same, the coide turns a gui panel green. if they are duifferent, the gui panel turns red. here is the code"
handles.uipanels = [handles.uipanel1, handles.uipanel2, handles.uipanel3, handles.uipanel4, handles.uipanel5, handles.uipanel6, handles.uipanel7, handles.uipanel8];
portlist = {'COM3'}; % , 'COM4', 'COM5', 'COM6'
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(tags{portidx}); %opens th eport
cleanups{portidx} = onCleanup(@() fclose(portlist{portidx}));
end
BOX = char(zeros(8,14)); % matrix to be populated with incoming serial data
TrueValueData = 'C:\Users\Administrator\Dropbox (*********)\********** Team Folder\Matlab\RFID chip reader\RfidChipData\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
pause(0.01)
% for n = 1:2
for portidx = 1 : nport
nbase = portidx * 2 - 1;
for n = nbase:nbase+1
if i>10 % positive reading
% readData = fscanf(tag);
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
instead of TrueValMat coming from an xlsread(), I want it to come from a string array. How would I get the rest of the code to function with this change?

  6 Comments

Show 3 older comments
Walter Roberson
Walter Roberson on 31 Dec 2019
Brace indexing is supported for string arrays, and always has been since string arrays were introduced in R2016b.
>> R = string(randi(9,20,20))
R =
20×20 string array
"6" "6" "6" "4" "7" "6" "9" "3" "8" "3" "6" "5" "2" "3" "6" "4" "6" "6" "7" "8"
"2" "5" "7" "8" "9" "2" "5" "4" "6" "7" "8" "8" "9" "7" "8" "3" "1" "5" "9" "5"
"6" "9" "3" "2" "9" "8" "9" "5" "4" "2" "3" "3" "1" "4" "9" "4" "5" "9" "7" "8"
"7" "4" "6" "4" "2" "7" "3" "7" "8" "5" "5" "5" "9" "2" "4" "2" "2" "3" "6" "4"
"1" "6" "4" "9" "9" "3" "6" "7" "1" "2" "9" "1" "9" "1" "7" "5" "6" "8" "9" "8"
"1" "9" "7" "8" "3" "9" "4" "1" "5" "4" "7" "1" "6" "8" "1" "6" "7" "2" "8" "8"
"1" "4" "5" "2" "2" "1" "1" "4" "7" "5" "6" "1" "9" "4" "7" "2" "4" "2" "6" "3"
"6" "8" "2" "6" "1" "1" "1" "5" "8" "9" "3" "1" "2" "5" "7" "2" "6" "8" "7" "4"
"9" "3" "2" "5" "8" "2" "6" "3" "5" "2" "5" "9" "9" "6" "1" "7" "3" "9" "3" "4"
"5" "5" "8" "6" "2" "5" "8" "5" "6" "1" "6" "9" "1" "1" "1" "9" "7" "6" "4" "8"
"7" "5" "3" "7" "1" "2" "2" "4" "4" "8" "8" "1" "1" "7" "6" "1" "1" "1" "1" "8"
"8" "2" "2" "5" "9" "3" "4" "3" "9" "7" "2" "7" "4" "9" "1" "8" "1" "5" "3" "4"
"7" "6" "9" "8" "7" "8" "2" "9" "7" "6" "8" "1" "8" "4" "8" "8" "8" "5" "9" "2"
"7" "5" "1" "3" "1" "9" "1" "1" "7" "1" "2" "5" "2" "7" "3" "8" "2" "2" "1" "9"
"9" "8" "9" "8" "6" "2" "9" "5" "9" "4" "1" "9" "7" "5" "7" "9" "9" "1" "5" "7"
"8" "7" "9" "4" "6" "3" "4" "5" "2" "4" "8" "2" "8" "9" "3" "8" "1" "5" "9" "3"
"8" "1" "6" "1" "5" "5" "2" "7" "5" "4" "6" "8" "9" "5" "6" "1" "3" "5" "3" "6"
"8" "4" "8" "5" "3" "7" "5" "5" "8" "2" "4" "4" "6" "4" "7" "6" "2" "7" "6" "2"
"8" "3" "6" "5" "5" "2" "5" "6" "9" "3" "2" "6" "9" "8" "2" "8" "1" "3" "5" "4"
"9" "4" "6" "8" "7" "9" "1" "1" "8" "7" "1" "3" "4" "5" "8" "6" "6" "1" "9" "6"
>> R{8,14}
ans =
'5'
avram alter
avram alter on 31 Dec 2019
The way I built my array is surrounded by braces.
C = {'0000478h4dj', '4502jn9u3mr84', '000274u5ll3y'};
This is a stupid question, but that changes things, right?
Walter Roberson
Walter Roberson on 31 Dec 2019
That is not a string array, that is a cell array of character vectors. And it does support {} indexing:
>> C = {'0000478h4dj', '4502jn9u3mr84', '000274u5ll3y'};
>> C{1,2}
ans =
'4502jn9u3mr84'

Sign in to comment.

Answers (0)

Sign in to answer this question.

Products


Release

R2019a