convert xlsread code to use a cell array

조회 수: 1 (최근 30일)
avram alter
avram alter 2019년 12월 30일
댓글: Walter Roberson 2019년 12월 31일
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
avram alter
avram alter 2019년 12월 31일
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 2019년 12월 31일
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'

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by