Output of values via a callback function

6 ビュー (過去 30 日間)
Tim
Tim 2023 年 2 月 7 日
回答済み: Steven Lord 2023 年 2 月 7 日
Hello all,
I am currently working with the data acquisition toolbox to record data from an accelerometer. This also works so far. However, I would like to plot this measurement data live so that the data runs smoothly and is not only updated 10 times per second (ScansAvailableFcnCount = 10 Hz). Conversely, this means that I have to save the data respectively output it from the function. This is exactly where I get stuck, because "data" is not output. I am aware that I did not specify the output value when I called the function, but I do not know where I do this or whether it works at all.
Maybe my approach is too complicated and someone has a simpler solution.
Thank you for your help,
many greetings
Tim
dq = daq('ni');
dq.Rate = 1000;
addinput(dq, 'Dev1', 'ai0', 'Voltage');
addinput(dq, 'Dev1', 'ai1', 'Voltage');
addinput(dq, 'Dev1', 'ai2', 'Voltage');
dq.ScansAvailableFcn = @(src,evt) readDataAvailable(src, evt);
start(dq, "Duration", seconds(5))
function data = readDataAvailable(src, ~)
[data, timestamps, ~] = read(src, src.ScansAvailableFcnCount, "OutputFormat", "Matrix");
end

採用された回答

Jan
Jan 2023 年 2 月 7 日
Callbacks do not have an output. Store obtained data in the UserData or ApplicationData of the calling object.
  2 件のコメント
Stephen23
Stephen23 2023 年 2 月 7 日
Jan
Jan 2023 年 2 月 7 日
This suggestion is smart also. Thanks.

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

その他の回答 (1 件)

Steven Lord
Steven Lord 2023 年 2 月 7 日
For this particular question you could addpoints to an animatedline inside the callback function. Each press of the button callback adds (x, x.^2) for the next integer value x to the line. I used the UserData property of the button to store the handle to the animated line and the next x value to be added to the plot.
I even added a pause statement (to give you the opportunity to click on some points) then used getpoints on the animatedline to retrieve the points the callback added to it.
L = animatedline(Marker = "o");
h = uicontrol(Style = "push", ...
UserData = struct('x', 1, 'L', L), ...
Callback = @addPointsToLine);
pause(10)
[x, y] = getpoints(L)
function addPointsToLine(thebutton, varargin)
newx = thebutton.UserData.x;
thebutton.UserData.x = newx+1;
addpoints(thebutton.UserData.L, newx, newx.^2);
end

カテゴリ

Help Center および File ExchangeCounter and Timer Input and Output についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by