Invalid data format for digital channel. Data must be of double or logical type and contain values of 0 or 1.

5 views (last 30 days)
I'm using the data acquisition toolbox with the NI 6009 data acquisition device to collect data from output, but I'm running into the problem described below.
This is the part of the code
This is the error occured while i was running the sumulation.
This is the full code
function saossb(block)
%SAOSSB S-Function for the DAQ Analog Output (Single Scan) block.
%
% MO 03-22-2016
% Copyright 2016 The MathWorks, Inc.
% The setup method is used to setup the basic attributes of the
% S-function such as ports, parameters, etc. Do not add any other
% calls to the main body of the function.
setup(block);
SetInputPortDataType(block);
SetInputPortDims(block);
end
%endfunction
%% Function: setup ===================================================
% Set up the S-function block's basic characteristics such as:
% - Output ports
% - Dialog parameters
% - Options
%
function setup(block)
% Parameters:
% 1:ObjConstructor,
% 2:NChannelsSelected,
% 3:NPorts,
% 4:SampleTime,
% 5:Device
% 6:Channels
block.NumDialogPrms = 6;
block.DialogPrmsTunable = {'Nontunable','Nontunable','Nontunable',...
'Nontunable','Nontunable','Nontunable'};
block.SampleTimes = [block.DialogPrm(4).Data 0];
% Register number of ports
block.NumOutputPorts = 0;
nPorts = block.DialogPrm(3).Data;
numChannels = block.DialogPrm(2).Data;
numPorts = daqblks.internal.getNumChannelPortsAndDimension(numChannels, nPorts);
block.NumInputPorts = numPorts;
% Specify if Accelerator should use TLC or call back into
% MATLAB file
block.SetAccelRunOnTLC(false);
block.SetSimViewingDevice(true);% no TLC required
% Allow multi dimensional signal support.
block.AllowSignalsWithMoreThan2D = false;
%% -----------------------------------------------------------------
%% Register methods called during update diagram/compilation
%% -----------------------------------------------------------------
%%
%% SetOutputPortDimensions:
%% Functionality : Check and set output port dimensions
block.RegBlockMethod('SetInputPortDimensions', @SetInputPortDims);
%%
%% PostPropagationSetup:
%% Functionality : Setup work areas and state variables. Can
%% also register run-time methods here
%% C-Mex counterpart: mdlSetWorkWidths
%%
block.RegBlockMethod('PostPropagationSetup', @DoPostPropSetup);
%% -----------------------------------------------------------------
%% Register methods called at run-time
%% -----------------------------------------------------------------
%%
%% Start:
%% Functionality : Called in order to initialize state and work
%% area values
block.RegBlockMethod('Start', @Start);
%%
%% Outputs:
%% Functionality : Called to generate block outputs in
%% simulation step
block.RegBlockMethod('Outputs', @Outputs);
%%
%% Terminate:
%% Functionality : Called at the end of simulation for cleanup
%%
block.RegBlockMethod('Terminate', @Terminate);
%%
%% WriteRTW:
%% Functionality : Called before running Rapid Accelerator mode
%% and other modes require codegen
%%
block.RegBlockMethod('WriteRTW', @WriteRTW);
end
%endfunction setup
%% DoPostPropSetup - Set up the Dwork elements.
function DoPostPropSetup(block)
end
%endfunction
%% SetOutputPortDims - Check and set output port dimensions
function SetInputPortDims(block)
% Parameters:
% 1:ObjConstructor,
% 2:NChannelsSelected,
% 3:NPorts,
% 4:SampleTime,
% 5:Device
% 6:Channels
nPorts = block.DialogPrm(3).Data;
numChannels = block.DialogPrm(2).Data;
[numPorts, dimension] = daqblks.internal.getNumChannelPortsAndDimension(numChannels, nPorts);
for channelCount = 1:numPorts
block.InputPort(channelCount).Dimensions = dimension;
end
end
%% SetOutputPortDataType - Check and set output port datatypes
function SetInputPortDataType(block)
for portCount = 1:block.NumInputPorts
block.InputPort(portCount).Complexity = 'Real';
block.InputPort(portCount).SamplingMode = 'Sample';
block.InputPort(portCount).DatatypeID = 0; % double
end
end
%endfunction SetOutputPortDataType
%% Start - Set up the environment by creating objects, initializing data.
function Start(block)
% Parameters:
% 1:ObjConstructor,
% 2:NChannelsSelected,
% 3:NPorts,
% 4:SampleTime,
% 5:Device
% 6:Channels
% Get the error strings.
errorStrings = daqblks.internal.getString('errorstrings');
% Get the block name.
blockName = get_param(block.BlockHandle, 'Name');
% Error if no device selected.
objConstructor = block.DialogPrm(1).Data;
if isempty(objConstructor)
error('%s for block %s',errorStrings.NoDevice, blockName);
else
%create daqObj and save it to UserData in BlockHandle
subSystemType = 'AnalogOutput';
measurementType = {'Voltage'};
checkClock = -1;
deviceList = daqblks.internal.findDevice(subSystemType, measurementType);
deviceDisplayString = block.DialogPrm(5).Data;
channelList = daqblks.internal.getChannelInfo(deviceList, deviceDisplayString, subSystemType, checkClock, measurementType);
if isempty(channelList)
error(errorStrings.DeviceInvalid, deviceDisplayString);
end
tableData = block.DialogPrm(6).Data;
[isSelected, channelID, ~, module, measurementType, range] ...
= daqblks.internal.parseAndUpdateTable(tableData, subSystemType);
if length(channelList) ~= length(isSelected)
error(errorStrings.DeviceModified, deviceDisplayString);
else
% Discrepancy is found in channel info. Something has been
% changed since the model was created
daqObj = eval(objConstructor);
daqblks.internal.addChannels(daqObj, subSystemType, channelList, ...
isSelected, channelID, module, measurementType, range);
set_param(block.BlockHandle, 'UserData', daqObj);
end
end
end
%endfunction Start
%% Outputs - Generate block outputs at every timestep.
function Outputs(block)
% Parameters:
% 1:ObjConstructor,
% 2:NChannelsSelected,
% 3:NPorts,
% 4:SampleTime,
% 5:Device
% 6:Channels
daqObj = get_param(block.BlockHandle, 'UserData');
% Errors out if daqObj is invalid
assert(isa(daqObj,'daq.Session') & isvalid(daqObj), 'Invalid session object');
if ~isempty(daqObj.Channels)
nPorts = block.DialogPrm(3).Data;
numChannels = block.DialogPrm(2).Data;
numPorts = daqblks.internal.getNumChannelPortsAndDimension(numChannels, nPorts);
if numPorts == 1
data = block.InputPort(numPorts).Data;
data = data.'; % It needs to be a row vector
else
data = zeros(1,numPorts);
for channelCount = 1:numPorts
data(channelCount) = [block.InputPort(channelCount).Data];
end
end
outputSingleScan(daqObj, data);
end
end
% endfunction Outputs
%% Terminate - Clean up
function Terminate(block)
% Delete the daqObj
daqObj = get_param(block.BlockHandle, 'UserData');
if isa(daqObj, 'daq.Session') && isvalid(daqObj)
delete(daqObj);
end
userData = [];
set(block.BlockHandle, 'UserData', userData);
end %endfunction
function WriteRTW(block)
% Get the block name.
blockName = get_param(block.BlockHandle, 'Name');
error(message('daq:daqblks:rapidAccelNotSupported', blockName));
end %endfunction

Answers (1)

KSSV
KSSV on 14 Jun 2022
The error is simple. Indexing of array in MATLAB should be positive integers or logicals.
A = rand(5,1) ;
A(1) % no error
ans = 0.0842
A(5) % no error
ans = 0.4660
A(logical(0)) % no error, as the index is logical; it gives empty matrix
ans = []
A(logical(1)) % no error, as the index is logical 1, it gives first element
ans = 0.0842
idx = A>0.2 % logical indices
idx = 5×1 logical array
0 1 1 1 1
A(idx) % no error
ans = 4×1
0.3932 0.7033 0.7665 0.4660
A(0) % error, here 0 is not a logical.
Array indices must be positive integers or logical values.
Similiarly, check your code for index where error is thrown. Convert the variable to logical.

Categories

Find more on Visualize and Interpret Parallel Link Project Analysis Results in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!