How can I plot a 2D variable with a slider for the 3rd dimension?

Hi!
I have a 32x1x120 variable (so I have 32 values in 120 instants of time), and I would like to plot the 32 values for time = 1, for time = 2, and eventually for time = 120. To not make 120 plots, I thought I could make a slider for the time dimension so that I could use one figure and slide through time for the 32 values, but I'm totally uncertain as to how to proceed.
So I would like something like this but with a slider for time below:
Thanks so much in advance.
Kind regards,
Guillem

 Accepted Answer

This demo produces data as you described and a slider is used to select the 3rd dimension of the data to be plotted.
data = rand(32,1,120);
uif = uifigure();
uax = uiaxes(uif,'Position', [20 100 500 300]);
bh = bar(uax, data(:,:,1));
hold(uax, 'off') % don't hold!
grid(uax, 'on')
n = size(data,3);
uis = uislider(uif,'Position',[50 50 450 3],...
'Value',0, ...
'Limits', [0,n], ...
'MajorTicks', 0:10:n, ...
'MinorTicks', []);
uis.ValueChangedFcn = @(h,~)bar(uax,data(:,:,round(h.Value)));
Alternatively you can use a local function to change the bar plot and the title to indicate the selected index.
data = rand(32,1,120);
uif = uifigure();
uax = uiaxes(uif,'Position', [20 100 500 300]);
bh = bar(uax, data(:,:,1));
title(uax, '1');
hold(uax, 'off') % don't hold!
grid(uax, 'on')
n = size(data,3);
uis = uislider(uif,'Position',[50 50 450 3],...
'Value',0, ...
'Limits', [0,n], ...
'MajorTicks', 0:10:n, ...
'MinorTicks', []);
uis.ValueChangedFcn = {@sliderChangedFcn, uax, data};
function sliderChangedFcn(h,~,uax,data)
value = round(h.Value);
bar(uax,data(:,:,value))
title(uax,num2str(value))
end

More Answers (0)

Categories

Find more on Develop Apps Programmatically in Help Center and File Exchange

Products

Release

R2019b

Community Treasure Hunt

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

Start Hunting!