Main Content

getBufferedData

Gets data from the real-time application instrument buffer

Since R2022b

Description

map_object = getBufferedData(instrument_object) gets data from a buffer in a variable on the MATLAB® development computer for real-time application instrument. Some data loss may occur if the Speedgoat® target computer does not have enough time to send the TCP packet with data for the buffer.

In normal (non-buffered) data mode, an instrument for a real-time application can be configured with a callback that gets executed each time data is available. The normal data mode operation of an instrument pushes the data to the callback.

In buffered data mode (when the instrument.BufferData flag is enabled), the callback configured for the instrument is not executed when data arrives. Instead, the real-time application stores the data and an app (for example, an instrument panel app) can retrieve the data by using the getBufferedData function. The call to this function returns the buffered data, then the function deletes the data from the instrument. The instrument continues to buffer new data until the next call to the getBufferedData function. The buffered mode operation of an instrument lets the app pull the data from the instrument.

An example situation for buffered data mode is the case when callbacks cannot be executed through the Python-MATLAB bridge. When you create an instrument from Python code, there is no way for the instrument to execute a callback in Python because the Python-MATLAB API does not support callbacks. The buffered data mode lets you get the data from the instrument by requesting it.

example

Examples

collapse all

Get buffered data for a real-time application instrument by using the getBufferedData function. The function gets data that is buffered from the time that you change the real-time application from normal (not buffered) data mode to buffered data mode by enabling the Instrument.BufferData flag. This flag only affects the operation of the getBufferedData function.

% Prepare target
tg = slrealtime;
mdlSTF = getSTFName(tg);
mdlName = 'slrt_ex_pendulum_100Hz';
mldatxName = 'slrt_ex_pendulum_100Hz.mldatx';
openExample(mdlName);
set_param(mdlName,"SystemTargetFile",mdlSTF);
slbuild(mdlName);
load(tg,mdlName);
removeAllInstruments(tg);

% Prepare slrealtime instrument object
instObj = slrealtime.Instrument(mldatxName);
instObj.addSignal('slrt_ex_pendulum_100Hz/Pendulum',1);

% Activate BufferData mode. Instrument data 
% will not be streamed to SDI.
instObj.BufferData = true;

% Add instrument object to target object
addInstrument(tg,instObj);

% Start simulation on target
start(tg);

% Wait for one second and retrieve buffered 
% data since start
pause(1);
logData = processBuffer(instObj);

% Wait for one second and retrieve buffered 
% data since last call to getBufferedData
pause(1);
logData = processBuffer(instObj);

% Stop simulation
stop(tg);

% Helper function
function logData = processBuffer(instObj)
   myMap = getBufferedData(instObj);
   logData = myMap.values;
   disp('New buffer data available:');
   struct2table(logData{1})
end

Input Arguments

collapse all

To create the instrument object, use the Instrument function.

Example: hInst

Output Arguments

collapse all

The map_object returns the buffered data from the getBufferedData function call.

Version History

Introduced in R2022b