Pass Workspace variable to ScansAvailableFcn listener in DataAcquisition object.

조회 수: 7 (최근 30일)
I am trying to pass workspace variables to a function handle called out in my data acquisition object. I have multiple variables that need to pass into and out of that function other than the DataAcquisition object. Do I have to pass the variable through the acquisition object struct to the function through the Userdata variable, or is there another way to pass it through when the ScansAvailableFcn is triggered by the ScansAvailableFcnCount? A simple example is shown below where I am trying to pass the Variable "Count" to the logDataTest function handle when the listener is triggered.
clear;
close all;
clc;
filename=input('Create filename ','s');
%initialize DAQ
d=daqlist;
s=daq("ni");
%adding analog inpput channel
addinput(s,d.DeviceID(4),2,'Current');
%sampling rate
s.Rate = 100; %(Hz)
s.ScansAvailableFcnCount=100;
%Variable to pass to logDataTest along with Data Acquisition object
Count=0;
s.ScansAvailableFcn=@logDataTest;
%edit duration later, want to run
start(s,"Continuous");%(s)
function logDataTest(s,Count)
[data,t,trigger] = read(s,s.ScansAvailableFcnCount,"OutputFormat","Matrix");
log=[t, data];
Count=Count+1;
The errors that read out when I try to run the data acquisition read:
Warning: Error occurred while executing the listener callback for event ElementsAvailable defined for class
daq.Buffer:
Undefined function 'plus' for input arguments of type 'matlabshared.asyncio.buffer.ElementsAvailableInfo'.
Error in logDataTest (line 4)
Count=Count+1;
Error in daq.interfaces.DataAcquisition>@(s,e)fcn(obj,e) (line 1476)
@(s,e)fcn(obj,e));
Error in matlabshared.asyncio.buffer.Buffer/notify (line 232)
notify@handle(obj, varargin{:});
Error in daq.Buffer/dataWrittenHandler (line 229)
notify(obj, 'ElementsAvailable',
matlabshared.asyncio.buffer.ElementsAvailableInfo(obj.NotificationScanCount));
Error in matlabshared.asyncio.buffer.Buffer>@(src,evt)obj.dataWrittenHandler(src,evt) (line 365)
obj.Channel, 'ElementsAvailable', @(src, evt) obj.dataWrittenHandler(src, evt));
Error in matlabshared.asyncio.buffer.internal.BufferChannel/dataWrittenHandler (line 428)
notify(obj, 'ElementsAvailable', matlabshared.asyncio.buffer.ElementsAvailableInfo(count));
Error in matlabshared.asyncio.buffer.internal.BufferChannel>@(src,evt)obj.dataWrittenHandler(src,evt) (line 407)
obj.InputStream, 'DataWritten', @(src, evt) obj.dataWrittenHandler(src, evt));
Error in asyncio.Channel/onDataReceived (line 487)
notify(obj.InputStream, 'DataWritten', ...
Error in asyncio.Channel>@(source,data)obj.onDataReceived() (line 425)
@(source, data) obj.onDataReceived());
  댓글 수: 1
chia cheng cheng
chia cheng cheng 2022년 1월 29일
i just the same problem using daq and GUI control.
but it seems not affect my signal measurement.
althought it is not affect my signal, it may still affect my measurement system.
Do you solve this problem? if you do, please let me know. I still not understand why this problem occurs ...

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

답변 (1개)

Walter Roberson
Walter Roberson 2022년 1월 29일
For a situation like this where you need to update a variable inside a callback, then you have a few choices:
  • use a variable in the base workspace
  • use a global variable
  • use UserData or application data attached to an object you can get a handle for
  • create a handle object and an update method and use parameterization to pass the handle in
  • define the update function as a nested function with a shared variable with the parent function

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by