Creating a plot that can be updated with a user slider control
8 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
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 Center 和 File Exchange 中查找有关 Discrete Data Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!