GUI Code XLabel Update

Hello, I am trying to create a gui. It will read analogvalue from arduino and plot it synchronous with current time value. However, Xlabels are shifting faster than graph. For example if the value at time 12:31:15 is 4.87, that value should shift to the left in alignmet with time label 12:31:15. Here is my code for plot button. You can see graph in the video that i attached. What is wrong? Thanks.
function pushbutton1_Callback(hObject, eventdata, handles)
global a k stopFlag;
arduinoPin = 'D7';
maxPoints = 100;
% Initialize the counter and stop flag
k = 1;
stopFlag = false;
while true
% Check if the stop button is pressed
if stopFlag
break; % Exit the loop if stopFlag is true
end
b = readVoltage(a, 'A0');
if b < 4
writeDigitalPin(a, arduinoPin, 1);
end
if b > 4
writeDigitalPin(a, arduinoPin, 0);
end
x = get(handles.axes1, 'UserData');
x = [x, b];
if length(x) > maxPoints
x = x(end - maxPoints + 1:end);
end
% Update x-axis with current time
timeString = datestr(now, 'HH:MM:SS');
xLabels = cellstr(get(handles.axes1, 'XTickLabel'));
xLabels = [xLabels(2:end); timeString];
axes(handles.axes1);
plot(x);
xlabel("Current time information");
ylabel("Sensor Voltage");
title("Time vs Sensor Voltage Graph", 'FontSize', 25, 'FontWeight', 'bold', 'Color', 'r');
axis([0 length(x) 0 7]);
set(gca, 'XTickLabel', xLabels);
set(handles.axes1, 'UserData', x);
k = k + 1;
drawnow; % Update the figure without clearing
pause(0.1);
end

Answers (0)

Categories

Products

Release

R2023b

Asked:

on 2 Dec 2023

Community Treasure Hunt

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

Start Hunting!