Effacer les filtres
Effacer les filtres

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

20 vues (au cours des 30 derniers jours)
John Carroll
John Carroll le 5 Avr 2024
Commenté : John Carroll le 8 Avr 2024
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.

Réponse acceptée

Voss
Voss le 6 Avr 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

Plus de réponses (0)

Catégories

En savoir plus sur Discrete Data Plots dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by