Creating a plot that can be updated with a user slider control

조회 수: 28 (최근 30일)
John Carroll
John Carroll 2024년 4월 5일
댓글: John Carroll 2024년 4월 8일
Hello
I am looking to create a graph that I can actively update. Currently I create a 2D variable of this form:
C( : , k ) = X + Y.
Using a for loop I step through "k" and create the variable line by line. When I graph this as a 3D plot it is of this form:
plot( A , B , C ). This gives me a heat map of the values of C(). I can also graph as a 2D graph in this form plot ( A , C ( DispIndex , : )). This allows me to take a single line plot and display at a single location on the 3D plot.
I would like to adjust the 2D plot in real time. I'm looking to create a slider on the graph that will let me slide through the values of DispIndex and update the graph in real time. I'd also like to display on the graph what value is in C() at DispIndex.
Currently I have to manually enter a value for DispIndex and change the value shown in the graph title manually and regraph. I'd like to automate this process.
Thanks in advance for your help.

채택된 답변

Voss
Voss 2024년 4월 6일
Here's one way, adjust as needed:
N = 25;
x = 1:N;
y = 1:N;
z = peaks(N);
idx = 1;
f = figure();
ax = gca();
surf(ax,x,y,z)
h_line = line( ...
'Parent',ax, ...
'XData',x, ...
'YData',idx*ones(N,1), ...
'ZData',z(idx,:), ...
'Color','r', ...
'LineWidth',2);
t = ax.Title;
t.String = sprintf('index = %d',idx);
h_slider = uicontrol( ...
'Parent',f, ...
'Style','slider', ...
'Units','normalized', ...
'Position',[0 0 1 0.035], ...
'Min',1, ...
'Max',N, ...
'Value',idx, ...
'SliderStep',[1 5]/(N-1), ...
'Callback',{@cb_slider,z,h_line,t});
function cb_slider(src,~,z,h,t)
val = round(src.Value);
h.YData(:) = val;
h.ZData = z(val,:);
t.String = sprintf('index = %d',val);
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by