Clear Filters
Clear Filters

How do I make it so my Plot keeps the same colours

34 views (last 30 days)
I am using a modified version of the live data acquistion that I made. I am having issues dealing with lagging/losing frames on my plot while plotting. After using the
I can no longer hold the same colours that my variables used to be and they are constantly switching. See below
If I click start + stop the colours then overlay overtop eachother but I am still wanting this to not occur. I have included the relavant code that I am modified from the original example apart from using a table to be able to select multiple channels and a calibration switch that just runs the data through a linear equation.
properties (Access = private)
DAQ % Handle to DAQ object
DAQMeasurementTypes = {'Voltage','IEPE','Audio'}; % DAQ input measurement types supported by the app
DAQSubsystemTypes = {'AnalogInput','AudioInput'}; % DAQ subsystem types supported by the app
DevicesInfo % Array of devices that provide analog input voltage or audio input measurements
TimestampsFIFOBuffer % Timestamps FIFO buffer used for live plot of latest "N" seconds of acquired data
DataFIFOBufferch1 double % Data FIFO buffer used for live plot of latest "N" seconds of acquired data
FIFOMaxSize = 1E+6 % Maximum allowed FIFO buffer size for DataFIFOBuffer and TimestampsFIFOBuffer
int double % int for the calibration
indices double % channels that are selected
calibrate logical % if calibration is turned off and on
data double = zeros(1000000,8); %data before calibration
methods (Access = private)
function scansAvailable_Callback(app, src, ~)
% Callback function executed on DAQ object ScansAvailable event
% Check if the app object is still valid
if ~isvalid(app)
% Read data from the DAQ object
[, timestamps, ~] = read(src, src.ScansAvailableFcnCount, 'OutputFormat', 'Matrix');
% read in slopes&intercepts from the live script = evalin('base','slopes'); = evalin('base','intercepts');
% Calibration
if app.calibrate
% calibratedData = double;
calibratedData = calibrateData(app,,,, app.indices);
% If calibration is turned off, use raw data
calibratedData =;
% Store continuous acquisition data in FIFO data buffers
buffersize = round(app.DAQ.Rate * app.TimewindowEditField.Value) + 1;
app.TimestampsFIFOBuffer = storeDataInFIFO(app, app.TimestampsFIFOBuffer, buffersize, timestamps);
app.DataFIFOBufferch1 = storeDataInFIFO(app, app.DataFIFOBufferch1, buffersize, calibratedData(:, app.indices));
% Plotting (Update the plot every N data points received)
updatePlotInterval = 10; % Adjust this value based on your preference
if mod(src.ScansAvailableFcnCount, updatePlotInterval) == 0
function calibratedData = calibrateData(~,data, slopes, intercepts, indices)
% Calibrate the data using slopes and intercepts
calibratedData = data;
for i = 1:max(indices)
calibratedData(:, i) = calibratedData(:, i) * slopes(:, i) + intercepts(:, i);
function logDataToFile(app, timestamps)
% Log data to file if LogRequested is true
latestdata = [timestamps,]';
fwrite(app.TempFile, latestdata, 'double');
if timestamps(1) == 0
app.TriggerTime = triggertime;
function updateLivePlot(app)
% Update the live plot
plot(app.LiveAxes, app.TimestampsFIFOBuffer, app.DataFIFOBufferch1)
% Add this line if you want to overlay multiple plots
hold(app.LiveAxes, 'on');
% Disable interactivity
if numel(app.TimestampsFIFOBuffer) > 1
xlim(app.LiveAxes, [app.TimestampsFIFOBuffer(1), app.TimestampsFIFOBuffer(end)])
Any Suggestions are greatly appreciated!

Accepted Answer

Jonas on 21 Feb 2024
for me this looks like an error that has nothing to do with the disableDefaultInteraction
% plot(app.LiveAxes, app.TimestampsFIFOBuffer, app.DataFIFOBufferch1)
% hold(app.LiveAxes, 'on');
repeatedly for updating makes the color jump to the next color each time after calling plot (since you used hold on to keep adding data);
if you dont want to change the color, specify it explicitly with the 'Color' Name-Value pair during the plot command?
if your plot data has mutiple columns, the you can either plot each col inidividually with the needed color, or you set the color index for the next color
% example for three channels
% example for wrong coloring
hold on;
% example for right coloring
hold on;
Jonas on 22 Feb 2024
concerning the multi channel recording and plotting, i ask myself why you even have to worry about plot colors. those are already implemented in most of the case. Without searching, I remember an app on file exchange called DAQx, which is exactly the same as you are coding, a multi channel recording app. I have programmed a similar app privatly, which is based on the same app as you are modifying.
concerning the set line you were asking for
each line object (here p), has stored style information etc. and also the plotting x and y data stored with it. adding the new data to the vector XData and YData, you tell make the line longer. since you have for each channel one line object, we have to add the correct channel of the new data (dataX2 and dataY2) to their respective line object
Connor on 23 Feb 2024
Thanks! It took me a bit of time because it wasnt updating in the way I wanted it too and was only displaying one channel but, I got it to work with a for loop!

Sign in to comment.

More Answers (0)




Community Treasure Hunt

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

Start Hunting!