Plotting analog input continuously on a given axis

5 ビュー (過去 30 日間)
Niraj Desai
Niraj Desai 2020 年 6 月 6 日
コメント済み: Niraj Desai 2020 年 6 月 10 日
I'm trying to understand the behavior of the Data Acquisition Toolbox when I try to acquire data from a National Instruments board and plot it continuously. My code is printed below. It works -- but only if I declare daqObj as a global variable. If I don't, then I don't get an error, but nothing happens. I'm using Matlab 2020a and Data Acquisition Toolbox 4.1 on a Windows 10 Pro desktop.
function [] = myTest(ax)
% ax is an axes in a GUI made with App Designer
global daqObj % <--- why do I need this?
daqObj = daq("ni");
addinput(daqObj,"Dev1","ai0","Voltage");
daqObj.Rate = 1000;
daqObj.ScansAvailableFcn = @plotMyData;
daqObj.ScansAvailableFcnCount = 5000;
start(daqObj,"Continuous")
function [] = plotMyData(obj,~)
[data,timestamps] = read(obj,obj.ScansAvailableFcnCount,"OutputFormat","Matrix");
plot(ax,timestamps,data)
end
end

採用された回答

Geoff Hayes
Geoff Hayes 2020 年 6 月 10 日
Niraj - if you don't declare daqObj as a global variable, then it will be a local variable. When your function "completes" (after calling start(...), then all local variables will be deleted/destoyed. If daqObj is local, then it will be destroyed and will stop trying to acquire data so this is probably why "nothing happens". But if you declare it as a global variable then it will "exist" outside of the function (so that other functions could access/share this variable) and so the data acquisition will work.
You mention that you are using App Designer, so why not make the daqObj a property/member of the GUI so that you don't have to declare this as global? That way, you can control from within the GUI when to start and stop the acquisition.
  1 件のコメント
Niraj Desai
Niraj Desai 2020 年 6 月 10 日
Geoff,
Thank you for this thoughtful answer. What you wrote makes a lot of sense. Thanks also for the suggestion about App Designer. Good idea.
Niraj

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAnalog Data Acquisition についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by