Slider which controls a bar plot
27 views (last 30 days)
Show older comments
Karl Philip Hahne
on 5 Feb 2021
Commented: Karl Philip Hahne
on 8 Mar 2021
Hello everyone,
i would like to get a slider which controls a bar plot.
so i got this, but instead of the gauge i would like to have a bar plot which is changing while i move the slider.
at the end i need 4 - 5 different bars which are chancing individually depending on different functions. and there is a same variable which is in each functions and this should be the input from the slider.
function sliderchanging
% Create figure window and components
fig = uifigure('Position',[100 100 350 275]);
cg = uigauge(fig,'Position',[100 100 120 120]);
cg.MajorTicks = [0:10:100];
cg.MajorTickLabels = {'0','10','20','30','40','50','60','70','80','90','100'};
sld = uislider(fig,...
'Position',[100 75 120 3],'ValueChangingFcn',@(sld,event) sliderMoving(event,cg));
end
% Create ValueChangingFcn callback
function sliderMoving(event,cg)
cg.Value = event.Value;
end
2 Comments
Adam Danz
on 7 Feb 2021
Could you elaborate on the goal? I don't understand how the slider should affect the bar plot.
Accepted Answer
Adam Danz
on 8 Feb 2021
Edited: Adam Danz
on 7 Mar 2021
Here's a demo. The key is to set bh (the bar handle) and initialValues (the initial y-values of the bars) prior to setting the slider callback function.
uif = uifigure();
uax = uiaxes(uif,'Position', [20 100 500 300]);
initialValues = [50 100 150];
bh = bar(uax, initialValues);
grid(uax, 'on')
uis = uislider(uif,'Position',[50 50 150 3],'Value',0, 'Limits', [0,500], 'MajorTicks', 0:100:500, 'MinorTicks', []);
uis.ValueChangedFcn = @(h,~)set(bh, 'YData', initialValues + h.Value);
6 Comments
Adam Danz
on 7 Mar 2021
Edited: Adam Danz
on 7 Mar 2021
Two problems with the callback function.
1) you need to pass the bar handle into the function.
2) the bigger problem is that you're still just adding a constant to all bar heights. You need to apply the a/b/c equations to the ydata.
It's not entirely clear how the original y values are supposed to be used in the conversions. Consider this snippet below a sketch to work from.
bh = bar(___);
uis.ValueChangedFcn = {@updateBarsFcn, bh};
function updateBarsFcn(h,~,bh)
% h is the slider handle, h.Value is the current slide value
% bh: bar handle, 1x3
y = zeros(size(bh.YData));
y(1) = 100 * (h.Value/100) * 10;
y(2) = (2*100+4*200) * h.Value;
y(3) = 0.5 * h.Value;
set(bh, 'YData', y)
end
More Answers (1)
Mario Malic
on 5 Feb 2021
Edited: Mario Malic
on 5 Feb 2021
Hello,
here's an answer to a very similar question.
3 Comments
Mario Malic
on 8 Feb 2021
Just a note from the heatmap solution to get you started. Heatmap chart requires a figure or uifigure to plot, whereas bar chart requires axes or uiaxes to plot.
See the documentation for bar and do the first example to start with.
Since this is a homework task, show us what have you done so far so we can help you with it.
See Also
Categories
Find more on Specifying Target for Graphics Output 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!