How can I update plots in a GUI from a background data queue?

23 次查看(过去 30 天)
I am writing a data acquisition GUI (using GUIDE) to acquire data from an unsupported data acquisition system. One module in my GUI allows the user to livestream data and view it on a continuously updating plot. My data acquisition code runs in the background using the parfeval function. Within that code, I send data to a background data queue every 0.1 seconds. I use the afterEach function associated with the background data queue to update plots in the GUI that show the streaming data.
The syntax for the afterEach function is
listener = afterEach(queue, funtocall)
where the documentation states that "All callback functions must accept data as single argument." They give the example:
afterEach(queue, @foo) expects a function handle @foo to a function of the form
function foo(data)
end
It turns out that (in my opinion), this is misleading. I was able to get my code running with the following:
%Define callback for start button that begins data streaming
function button_StartStreaming_Callback(hObject, eventdata, handles)
% Define some data acquisition parameters
scansAvailableFcnCount=500; %every .1 seconds when sampling at 5000Hz
% daqConfig= structure containing daq parameters
% Configure background data queue
handles.q = parallel.pool.DataQueue;
L=afterEach(handles.q,@(data) dataCapture(handles,data)); %<---THIS IS THE LINE OF INTEREST
% Start continuous background data acquisition
f = parfeval(backgroundPool,@runDAQ,0,handles.q,scansAvailableFcnCount,daqConfig);
end
where my dataCapture function is
function dataCapture(handles,data)
% Extract data sent from parfeval function (@runDAQ)
% Store data in a persistent buffer
% Update the plots in the GUI using the handles structure
end
You can see that my callback function for the afterEach command accepts TWO inputs, which is incredibly useful, because the data variable does not (and cannot) contain the handles to all my GUI components. My code works, so my question is not about the code, but rather about the documentation. Did I misunderstand the documentation (where they say the callback function can only accept a single argument) or is this something that needs to be corrected/clarified?

采纳的回答

Voss
Voss 2024-4-3,16:47
编辑:Voss 2024-4-3,16:53
"You can see that my callback function for the afterEach command accepts TWO inputs"
That's not really true. This anonymous function is the afterEach callback, and it accepts only one input:
@(data) dataCapture(handles,data)
It accepts a single input argument, data, and passes it along with handles to another function, dataCapture, which accepts two input arguments:
function dataCapture(handles,data)
That's consistent with the documentation because the afterEach callback in this case is the anonymous function that takes only one input.
What you have done is to use an anonymous function to pass additional arguments to another function, which is a perfectly valid way to get around the single-input limitation.
  2 个评论
JanetteFromLafayette
JanetteFromLafayette 2024-4-3,16:52
Ok, this is revealing the fact that I don't have my brain completely wrapped around the concept (or maybe the terminology) of anonymous functions, even though I use them. Thanks for the explanation.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Parallel for-Loops (parfor) 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by