Borrar filtros
Borrar filtros

Output of values via a callback function

11 visualizaciones (últimos 30 días)
Tim
Tim el 7 de Feb. de 2023
Respondida: Steven Lord el 7 de Feb. de 2023
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

Respuesta aceptada

Jan
Jan el 7 de Feb. de 2023
Callbacks do not have an output. Store obtained data in the UserData or ApplicationData of the calling object.
  2 comentarios
Jan
Jan el 7 de Feb. de 2023
This suggestion is smart also. Thanks.

Iniciar sesión para comentar.

Más respuestas (1)

Steven Lord
Steven Lord el 7 de Feb. de 2023
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

Categorías

Más información sobre View and Analyze Simulation Results en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by