Updating a plot with a slider
Show older comments
Hello,
I have engine cylinder pressure data for many cycles. I would like to include a slider into the figure to change the plot to that of respective cycle. I tried to do this for the first 7 cycles. Although I do not get any error, the plot is not updated when I move the slider. Thanks in advance!
base_filename = 'processed_20250117_Measurement_7_ID'; %base file name for measurement 7
startID = 0;
endID = 214; %adjust based on the cycle number
data = cell(1, endID-startID + 1);
for id = startID:endID
filename = sprintf('%s%d.txt', base_filename, id);
data{id+1} = readtable(filename,VariableNamingRule="preserve");
end
crank_angle = cell(1,3);
cyl_pressure = cell(1,3);
for i = 1:7
crank_angle{i} = data{i}.("CAD[deg]");
cyl_pressure{i} = data{i}.("Pcyl[bar]");
end
fig = figure('Name', 'Cylinder pressure over crank angle', 'NumberTitle','off');
ax = axes('Parent', fig, 'Position', [0.1, 0.3, 0.8, 0.6]);
plot(ax, crank_angle{1}, cyl_pressure{1}, 'LineWidth', 1.5)
xlabel(ax, 'Crank angle [°CA]')
ylabel(ax, 'Cylinder pressure [bar]')
title(ax, 'Cylinder pressure over crank angle')
grid(ax, 'on')
slider = uicontrol('Style', 'slider','Min',1,'Max',7,'SliderStep',[1/6, 1/6],'Value',1, 'Callback', @updatePlot);
function updatePlot(src,~)
cycleID = round(src.Value);
crank_angle = cell(1,7);
cyl_pressure = cell(1,7);
plot(crank_angle{cycleID}, cyl_pressure{cycleID}, 'LineWidth', 1.5)
end
Accepted Answer
More Answers (0)
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!