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

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
disableDefaultInteractivity(app.LiveAxes);
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
end
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)
return
end
% Read data from the DAQ object
[app.data, timestamps, ~] = read(src, src.ScansAvailableFcnCount, 'OutputFormat', 'Matrix');
% read in slopes&intercepts from the live script
app.sl = evalin('base','slopes');
app.int = evalin('base','intercepts');
% Calibration
if app.calibrate
% calibratedData = double;
calibratedData = calibrateData(app,app.data, app.sl, app.int, app.indices);
else
% If calibration is turned off, use raw data
calibratedData = app.data;
end
% 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
updateLivePlot(app);
end
end
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);
end
end
function logDataToFile(app, timestamps)
% Log data to file if LogRequested is true
latestdata = [timestamps, app.data]';
fwrite(app.TempFile, latestdata, 'double');
if timestamps(1) == 0
app.TriggerTime = triggertime;
end
end
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
disableDefaultInteractivity(app.LiveAxes);
if numel(app.TimestampsFIFOBuffer) > 1
xlim(app.LiveAxes, [app.TimestampsFIFOBuffer(1), app.TimestampsFIFOBuffer(end)])
end
end
Any Suggestions are greatly appreciated!

 Accepted Answer

for me this looks like an error that has nothing to do with the disableDefaultInteraction
using
% 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
data1=[1:10;2:11;3:12]';
data2=[10:-1:1;11:-1:2;12:-1:3]';
% example for wrong coloring
figure;
plot(1:10,data1);
hold on;
plot(10:19,data2);
% example for right coloring
figure;
plot(1:10,data1);
hold on;
ax=gca;
ax.ColorOrderIndex=1;
plot(10:19,data2);

7 Comments

Hi Jonas,
Thanks for the help, I had initially used the disable default interactivity to remove the lag that would occur when hovering your mouse over the axes during data collection. Do you know a way that would help my code run a bit more efficiently when it comes to plotting?
Thanks and I appreciate the help a lot!
most of the time it is faster to update the plot lines instead of replotting all
e.g.
dataX1=[1:10; 1:10; 1:10]';
dataY1=[1:10; 1:10; 1:10]'+(1:3);
p=plot(dataX1,dataY1);
dataX2=[11:20; 11:20; 11:20]';
dataY2=[4 5 8 5 2 3 4 5 3 1;
4 5 6 2 3 5 6 2 3 4;
2 35 1 2 4 5 1 2 4 5]';
% if you update often, you may do not have the old data directly, thats why
% i do not use dataX1 and dataY1 directly here
for lineNr=1:numel(p)
oldXData=p(lineNr).XData;
oldYData=p(lineNr).YData;
set(p(lineNr),'XData',[oldXData,dataX2(:,lineNr)'],'YData',[oldYData,dataY2(:,lineNr)']);
end
this should be faster than using the hold on command and adding "new lines", since it adds additional line handles for each plot command. when updating the existing line handles, the number of handles stays the same all the time (depending on your number of channels)
Connor
Connor on 21 Feb 2024
Edited: Connor on 21 Feb 2024
Thanks! I am having a bit of trouble following the set line of code. Does it remove the "old data" and only plot the new (the difference) between the two variables?
and because the code I am running is continuily updating I have been having problems using things like "set" or "animatedLine" & "addlines" and whatnot. I am not sure why these have been as issue. I have been trying to work with ChatGPT and had had little success in being able to plot with my updateplot function. Thanks!
I also Just noticed that you had made something similiar to what I am trying to make. Were there any resources that you used that you would suggest?
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
set(p(lineNr),'XData',[oldXData,dataX2(:,lineNr)'],'YData',[oldYData,dataY2(:,lineNr)']);
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
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)

Products

Release

R2023b

Asked:

on 20 Feb 2024

Commented:

on 23 Feb 2024

Community Treasure Hunt

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

Start Hunting!