Tabulation of plot data
5 views (last 30 days)
Good day. I am new to MATLAB. I am making a tool that will plot and tabulate data observed from sludge settling (SV45) but I am having trouble trying to make a table that will store data from a plot at specific points in time. The x-axis is the time variable (in seconds) and the y-axis is the sludge level. The points in time of the plot I want to tabulate are the following: 0, 30, 60, 120, 180, 240, 300, 600, 900, 1200, 1800, and 2700.
Any help on this would be much appreciated. Happy holidays to everyone.
This is currently what I am working on the live editor:
1. Load Sample Image (Serves as background reference)
This example uses the reference image example.jpg.
refImage = imread("example.jpg");
This image is of an empty glass with a resolution of 640 x 480.
Alternatively, you can capture this image using the Acquire Webcam Image task.
2. Set up Output Window (Plot GUI)
Set up the figure to plot the sludge level changes in real time.
fig = figure("Name","Sludge level tracker","Visible","off");
t = tiledlayout(2,2,"Parent",fig);
t.Padding = 'none';
t.TileSpacing = 'none';
ax1 = nexttile(t);
ax2 = nexttile(t,[2 1]);
title('Sludge level vs Time',"Parent",ax2);
xlabel('Time (in s)',"Parent",ax2);
ylabel('Sludge level (normalized)',"Parent",ax2);
ax3 = nexttile(t);
h = animatedline(ax2,"Color","red","LineWidth",3);
webcamLETFig = figure("Name","Acquire Webcam Image Output");
3. Plot Sludge Level Change
To determine the change in sludge level, use image subtraction. Subtract each sample image from the reference image. To capture the sample images,use the Acquire Webcam Image task.
Ensure the following in the Live Task -
Select a webcam from the webcams connected to your computer.
Clear the Show image check box to prevent displaying the image in each loop iteration.
Turn off the Autorun feature if you want the live task to execute only when the entire script is executed. To disable autorun, click the autorun icon.
Select Live preview to view the live video from the webcam while adjusting webcam properties.
Do the following -
Capture images using the Acquire Webcam Image task in a loop. You can vary the time for which the loop executes by entering the time in seconds in the textbox.
Subtract each sampleImage from the refImage to get the diffImage.
Extract the the RGB components from diffImage and convert the image to grayscale to reduce the noise in the image.
Find the mean pixel intensity value across each row of the image. The mean pixel intensity is used to find the image pixel corresponding to the top of the sludge level.
Define a window size and a threshold for the intensity by moving the slider.
Find the difference in the instensities between the n+10th pixel and nth pixel in the windowSize. 'n' is the row index and 10 is the window size used in this example. If the intensity of the pixel value is greater than the threshold intensity, store the pixel value as the sludge level.
Plot the sludge level against time.
% Start time-stamp timer for plotting sludge level against time
t = tic;
while(toc(t) < 2700)
% Read the image captured from the webcam
% Make the "Acquire Webcam Image Output" figure the active figure
% Connect to selected webcam
cam = webcam("TOSHIBA Web Camera - FHD",...
% Acquire webcam image
img = snapshot(cam);
% Show acquired image
% Clear existing webcam connection
sampleImage = img;
% Store the relative timestamp
tStamp = toc(t);
% Subtract the sample image from the reference image
% 'imsubtract' from the Image Processing Toolbox can be used here
diffImage = refImage - sampleImage;
dims = size(diffImage);
% Extract RGB components from the difference image
R = diffImage(:,:,1);
G = diffImage(:,:,2);
B = diffImage(:,:,3);
% Convert the difference image to grayscale
greyDiffImage = zeros(dims(1),dims(2),'uint8');
for row = 1:dims(1)
for col = 1:dims(2)
greyDiffImage(row,col) = (0.2989 * R(row,col)) + (0.5870 * G(row,col)) + (0.1140 * B(row,col));
% Find mean pixel intensity value across each row of the image
rowMean = zeros(1,dims(1));
for row = 1: dims(1)
rowMean(row) = mean(greyDiffImage(row,:));
% Find the image pixel corresponding to the top of sludge level
% Find the difference instesities between the n+10th and nth pixel.
% Store this pixel value as the sludge level, if this intesnity is greater than a threshold
windowSize = 10;
intensityThreshold = 15;
sludgeLevelPixel = dims(1);
for rowIndex = 1:length(rowMean)-windowSize
if rowMean(rowIndex+windowSize) - rowMean(rowIndex) > intensityThreshold
sludgeLevelPixel = rowIndex;
% Display images
% Make the "Sludge Level Tracker" figure the active figure
% Display the reference image
% Display the current sludge level
% Plot sludge level vs/ time
addpoints(h,tStamp,abs(1 - (sludgeLevelPixel/dims(1))));
fig.Visible = "on"';
% Tabulate plot data
for k = 1 : length(sludgeLevelPixel)
fprintf('(x(%d), y(%d)) = (%.4f, %.4f)\n',...
k, k, sludgeLevelPixel(k), tStamp(k));
Sivani Pentapati on 7 Jan 2022
Based on my understanding, you want to capture the sludge level information at a list of time stamps and store them in a table. You could create an array which stores all the timestamps and check if current value of tStamp exists in the array using ismember function in each iteration. If yes, store the data in the table using writetable function.
Hope this helps!