Is there a way to control two subplots with one slider?

6 ビュー (過去 30 日間)
Derrell Dsouza
Derrell Dsouza 2022 年 6 月 1 日
回答済み: Geoff Hayes 2022 年 6 月 2 日
Can anyone demonstrate a way to update both subplots with one uicontrol slider?
In my case I have two matlab arrays with size (1024,128,20). I want to imshow 2d images of size 1024 x 128 for each of the third dimension using a slider and only one slider updating both subplots.
Thanks in advance
  2 件のコメント
Derrell Dsouza
Derrell Dsouza 2022 年 6 月 1 日
Please check the code below
%%
close all;
set(0,'defaultAxesFontSize',14)
set(0,'DefaultFigureWindowStyle','docked')
data = rand(3,1024,64,20);
nslice = size(data,4);
nidx = size(data,1);
if nslice == 1
sampling = 1; % in some of my data nslice would be 1 so I would like my slider to not give me error in such a case
else
sampling = 5;
end
idx = 1;
slice = 1;
h= struct;
h.f = figure(1);
x_axis = linspace(0,0.5,size(data,3));
y_axis = linspace(0,70,size(data,2));
set(h.f,'doublebuffer','on')
h.data = data;
inst_data = squeeze(data(nidx,:,:,slice));
h.ax(1) = subplot(1,2,1,'Parent',h.f,'Units','Normalized');
imagesc(h.ax(1),x_axis,y_axis, inst_data)
colorbar;
colormap(h.ax(1),'gray')
caxis(h.ax(1),[min(abs(inst_data(:))),max(abs(inst_data(:)))])
xlabel('xaxis')
ylabel('yaxis')
title('figure 1')
h.ax(2) = subplot(1,2,2,'Parent',h.f,'Units','Normalized');
log_data = log(inst_data);
imagesc(h.ax(2),x_axis,y_axis, log_data)
colorbar;
colormap(h.ax(2),'jet')
caxis(h.ax(2),[-max(abs(log_data(:))),max(abs(log_data(:)))])
xlabel('xaxis')
ylabel('yaxis')
title('figure 2')
sgtitle({'Sample data ', ['Slice: ',num2str(slice),' index: ', num2str(idx)]},'FontSize',16,'FontWeight','Bold')
h.slider1=uicontrol('Parent',h.f,...
'Units','Normalized',...
'Position',[0.1 0.06 0.8 0.05],...
'Style','slider',...
'SliderStep',[0.05,0.1],...
'Min',1,'Max',nslice,'Value',1,...
'Callback',{@slider_Callback,data,x_axis,y_axis});
txt1 = 'Slice';
txt2 = 'Index';
annotation('textbox', [0.05 0.07 0.05 0.04], 'string', txt1);
annotation('textbox', [0.05 0.021 0.05 0.04], 'string', txt2);
h.slider2=uicontrol('Parent',h.f,...
'Units','Normalized',...
'Position',[0.1 0.02 0.8 0.05],...
'Style','slider',...
'SliderStep',[0.50,0.50],...
'Min',1,'Max',nidx,'Value',1,...
'Callback',{@slider_Callback,data,x_axis,y_axis});
guidata(h.f,h)
%%
function slider_Callback(hObject,eventdata,data,x_axis,y_axis)%#ok<INUSD>
h=guidata(hObject);%retrieve struct
slice_i = round(get(h.slider1, 'Value'));
idx_i = round(get(h.slider2,'Value'));
inst = squeeze(data(idx_i,:,slice_i));
colormap(h.ax(1),'gray')
caxis(h.ax(1),[min(abs(inst(:))),max(abs(inst(:)))])
h.ax(1) = imagesc(x_axis,y_axis,inst); colorbar
xlabel('xaxis')
ylabel('yaxis')
title('figure 1')
lg = log(inst);
colormap(h.ax(2),'jet')
caxis(h.ax(2),[-max(abs(lg(:))),max(abs(lg(:)))])
h.ax(2) = imagesc(x_axis,y_axis, lg ); colorbar;
sgtitle({'Sample data ', ['Slice: ',num2str(slice_i),' index: ', num2str(idx_i)]},'FontSize',16,'FontWeight','Bold')
xlabel('xaxis')
ylabel('yaxis')
title('figure 2')
drawnow
end
% use nested functions to add arguments to callback functions

サインインしてコメントする。

採用された回答

Geoff Hayes
Geoff Hayes 2022 年 6 月 2 日
@Derrell Dsouza - I think the issue is with your call to imagesc where you are not specifiying the axes upon which to draw the image. In fact, the slider callback is doing the following
h.ax(1) = imagesc(x_axis,y_axis,inst); colorbar
% other code
h.ax(2) = imagesc(x_axis,y_axis, lg ); colorbar;
which replaces the handles to the axes with the image object (while incorrect, because the h isn't actually updated via guidata then the axes handles are maintained for subsequent calls to this function).
The above code can be changed to specify which axes you want to update
imagesc(h.ax(1), x_axis,y_axis,inst); colorbar
% other code
imagesc(h.ax(2), x_axis,y_axis, lg ); colorbar;
The above should fix the issue and so both subplots should be updated (it worked for me). I do recommend placing all of this code within a (main) function rather than using a script so that your callbacks have access to the local variables defined in the main function (like h)...this avoids the need for calls to guidata.

その他の回答 (0 件)

カテゴリ

Find more on Migrate GUIDE Apps in Help Center and File Exchange

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by