我有一个程序可以将 0 写入外部数据采集设备的所有数字端口。我能够访问这些二进制值 0 或 1 作为工作区变量中的表写入端口。然后我将每个表中的每个位放入一个表中。因此,如果每个表都有一个 0 写入端口,那么单个 1x4 表将显示 0 0 0 0。我的目标是能够创建一个二进制计数器,在将所有 0 都输入到端口后,程序将计数到十进制的 15,相当于从二进制 0000 计数到二进制 1111.
目前我将全0的表数组转换为双精度数组。接下来,我将单元格中分隔的双精度数组 0 0 0 0 转换为位于单个单元格中的单个数字 0。然后我可以添加/增加值,因为它不是一个表,它返回一个 1。然后我将这个十进制数转换为一个二进制数,它等于 0001,即十进制表示法中的 1。我的问题是如何从单个单元格中找到的二进制数中提取每个数字,然后将其写入每个端口的现有表中。
如果对此有替代方案或任何建议,请告诉我这会有很大帮助,谢谢!
附件是我下面的全部代码:
BDaq = NET.addAssembly('Automation.BDaq');
deviceDescription = 'USB-5801,BID#0';
startPort = int16(0); % Writing data to Port 0
portCount = int16(4); %Writing digital data up to a certain number of Ports
instantDoCtrl = Automation.BDaq.InstantDoCtrl();
instantDoCtrl.SelectedDevice = Automation.BDaq.DeviceInformation(deviceDescription);
%data = NET.createArray('System.Byte', int16(32));
data = NET.createArray('System.Byte', portCount);
errorcode = instantDoCtrl.Read(startPort, portCount, data);
fileout = 'DataA.csv';
% open output file
fid=fopen(fileout,'w'); %Data is not appended
for i = 0:(portCount - 1)
fprintf('Input a bit value for Port %d',...
startPort + i);
strData = input('(for example, 0 or 1)\n', 's'); %Input in a binary number
strData = System.String(strData);
strData = bin2dec(char(strData));
data.Set(i, strData);
fprintf(fid,'Port %d: %8f ',i, data.Get(i));
% fprintf('Port %d: %8f ',i, data.Get(i))
%
disp('Digital output completed!');
% % Read back the DO status.
%
end
%Data Written to Ports
filename = 'C:\Users\kevin\OneDrive - Stony Brook University\Documents\USB-5801\DataA.csv';
formatSpec = '%4s%3s%9s%5s%3s%9s%5s%3s%9s%5s%3s%9s%s%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'TextType', 'string', 'ReturnOnError', false);
dataArray{1} = strtrim(dataArray{1});
dataArray{4} = strtrim(dataArray{4});
dataArray{7} = strtrim(dataArray{7});
dataArray{10} = strtrim(dataArray{10});
dataArray{13} = strtrim(dataArray{13});
fclose(fileID);
raw = repmat({''},length(dataArray{1}),length(dataArray)-1);
for col=1:length(dataArray)-1
raw(1:length(dataArray{col}),col) = mat2cell(dataArray{col}, ones(length(dataArray{col}), 1));
end
numericData = NaN(size(dataArray{1},1),size(dataArray,2));
for col=[2,3,5,6,8,9,11,12]
% Converts text in the input cell array to numbers. Replaced non-numeric text with NaN.
rawData = dataArray{col};
for row=1:size(rawData, 1)
% Create a regular expression to detect and remove non-numeric prefixes and suffixes.
regexstr = '(?<prefix>.*?)(?<numbers>([-]*(\d+[\,]*)+[\.]{0,1}\d*[eEdD]{0,1}[-+]*\d*[i]{0,1})|([-]*(\d+[\,]*)*[\.]{1,1}\d+[eEdD]{0,1}[-+]*\d*[i]{0,1}))(?<suffix>.*)';
try
result = regexp(rawData(row), regexstr, 'names');
numbers = result.numbers;
% Detected commas in non-thousand locations.
invalidThousandsSeparator = false;
if numbers.contains(',')
thousandsRegExp = '^[-/+]*\d+?(\,\d{3})*\.{0,1}\d*$';
if isempty(regexp(numbers, thousandsRegExp, 'once'))
numbers = NaN;
invalidThousandsSeparator = true;
end
end
% Convert numeric text to numbers.
if ~invalidThousandsSeparator
numbers = textscan(char(strrep(numbers, ',', '')), '%f');
numericData(row, col) = numbers{1};
raw{row, col} = numbers{1};
end
catch
raw{row, col} = rawData{row};
end
end
end
rawNumericColumns = raw(:, [2,3,5,6,8,9,11,12]);
rawStringColumns = string(raw(:, [1,4,7,10,13]));
DataA = table;
DataA.Port = rawStringColumns(:, 1);
DataA.VarName2 = cell2mat(rawNumericColumns(:, 1));
DataA.PortName0 = cell2mat(rawNumericColumns(:, 2));
DataA.Port1 = rawStringColumns(:, 2);
DataA.VarName5 = cell2mat(rawNumericColumns(:, 3));
DataA.PortName1 = cell2mat(rawNumericColumns(:, 4));
DataA.Port2 = rawStringColumns(:, 3);
DataA.VarName8 = cell2mat(rawNumericColumns(:, 5));
DataA.PortName2 = cell2mat(rawNumericColumns(:, 6));
DataA.Port3 = rawStringColumns(:, 4);
DataA.VarName11 = cell2mat(rawNumericColumns(:, 7));
DataA.PortName3 = cell2mat(rawNumericColumns(:, 8));
DataA.VarName13 = rawStringColumns(:, 5);
A = DataA; %Data placed in table after assinging bits to Ports
Portname1 = A(:,1:2); %Provides Port number 0
Port1 = A(1,3); %Provides bit assigned to the digital Port 0
Portname2 = A(:,4:5); %Provides Port number 1
Port2 = A(1,6); %Provides bit assigned to the digital Port 1
Portname3 = A(:,7:8); %Provides Port number 2
Port3 = A(1,9); %Provides bit assigned to the digital Port 2
Portname4 = A(:,10:11); %Provides Port number 3
Port4 = A(1,12); %Provides bit assigned to the digital Port 3
%4 bit Counter for Digital Outputs
Initial = A{:,[3 6 9 12]}; %Input all 0's for Digital Output Ports
if Initial == 0 %If all 0's are entered perform if task
Sequence1 = [Port1 Port2 Port3 Port4]; %0 0 0 0
Port1_cell = table2array(Sequence1); %Places the bit of the ports from a table into a double variable
Y = str2num(strrep(num2str(Port1_cell), ' ', '')) %Converts the double variable into a single number
A1 = Y +1; %increments the number
D1 = dec2bin(A1,4) %Displays as binary value