Updating a plot with a slider
9 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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
0 Commenti
Risposta accettata
Harald
il 4 Feb 2025
Hi,
if you set a breakpoint in the updatePlot function, you will notice that crank_angle and cyl_pressure are cell arrays with empty double arrays. While plots are usually overwritten, MATLAB is apparently smart and does not do so when x- and y-values are empty arrays.
You will need to ensure that crank_angle and cyl_pressure become properly available in the updatePlot function. While I cannot execute your code, I'd try adjusting lines like this (differences in bold):
slider = uicontrol('Style', 'slider','Min',1,'Max',7,'SliderStep',[1/6, 1/6],'Value',1, 'Callback', @(src, evt) updatePlot(src, crank_angle, cyl_pressure));
and
function updatePlot(src, crank_angle, cyl_pressure)
Best wishes,
Harald
Più risposte (0)
Vedere anche
Categorie
Scopri di più su 2-D and 3-D Plots in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!