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

 채택된 답변

Harald
Harald 2025년 2월 4일

0 개 추천

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

댓글 수: 1

Rufat
Rufat 2025년 2월 4일
Hello Harald,
thanks for your reply. It solved my problem.
Kind regards,
Rufat

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

질문:

2025년 2월 4일

댓글:

2025년 2월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by