Creating a plot that can be updated with a user slider control
17 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Voss
on 6 Apr 2024
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
More Answers (0)
See Also
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!