2014-01-22 59 views
2

我有一个576x576x150矩阵。每个576x576集代表一个图像。当我想绘制一个帧我使用绘图命令做到这一点:MATLAB在图中添加滑块

figure(1); 
imshow(B(:,:,45),[]) % plots frame 45 
title('45') % tells frame number 

但是我想滑块添加到情节,所以我可以从1-150帧figure.I”中移动我看过使用uicontrol的人的例子,但我不知道如何编写代码。除此之外,我想在图的上方标题,告诉我帧号。

+0

相关:http://stackoverflow.com/questions/15975758/matlab-simple-slider-on-a-figure – herohuyongtao

回答

1

这里是我如何做到这一点。我喜欢保留一个绘图功能,因此您不需要在别处回收命令。您可以用function test(B)替换前两行,以使用您自己的B矩阵。这段代码很容易扩展。你也会想玩布局来适应你的目的。

function test 
B=rand(576,576,150); 

fig=figure(100); 
set(fig,'Name','Image','Toolbar','figure',... 
    'NumberTitle','off') 
% Create an axes to plot in 
axes('Position',[.15 .05 .7 .9]); 
% sliders for epsilon and lambda 
slider1_handle=uicontrol(fig,'Style','slider','Max',150,'Min',1,... 
    'Value',2,'SliderStep',[1/(150-1) 10/(150-1)],... 
    'Units','normalized','Position',[.02 .02 .14 .05]); 
uicontrol(fig,'Style','text','Units','normalized','Position',[.02 .07 .14 .04],... 
    'String','Choose frame'); 
% Set up callbacks 
vars=struct('slider1_handle',slider1_handle,'B',B); 
set(slider1_handle,'Callback',{@slider1_callback,vars}); 
plotterfcn(vars) 
% End of main file 

% Callback subfunctions to support UI actions 
function slider1_callback(~,~,vars) 
    % Run slider1 which controls value of epsilon 
    plotterfcn(vars) 

function plotterfcn(vars) 
    % Plots the image 
    imshow(vars.B(:,:,get(vars.slider1_handle,'Value'))); 
    title(num2str(get(vars.slider1_handle,'Value'))); 
+0

我取代了第二线与负载( 'frames.mat'),它包含一个B(576,576,150)矩阵,但它不工作:S – Jorge

+0

这很奇怪!你遇到了什么错误? – David

+0

没有错误,但所有的帧都是白色的!也许是一个开窗的问题。在我使用之前:'a = min(min(min(B,[],1)));' 'b = max(max(max(B,[],1)));'然后'imshow (B(:,:,frame),[ab])'但我不知道如何将它添加到你的代码中(如果这是问题)。 – Jorge

1

这个想法是使用uicontrol()来启用滑动/滚动。

下面的代码是用于滚动(由Evan Brooks创建,您可以将它修改为滑动):

function scrollfigdemo 

% create new figure window 
f = figure; 
set(f,'doublebuffer', 'on', 'resize', 'off') 

% set columns of plots 
cols = 2; 

% create 5 data sets to plot 
x=0:1e-2:2*pi; 
y{1}=sin(x); 
y{2}=cos(x); 
y{3}=tan(x); 
y{4}=x.^2; 
y{5}=x.^3; 

% determine required rows of plots 
rows = ceil(length(y)/cols); 

% increase figure width for additional axes 
fpos = get(gcf, 'position'); 
scrnsz = get(0, 'screensize'); 
fwidth = min([fpos(3)*cols, scrnsz(3)-20]); 
fheight = fwidth/cols*.75; % maintain aspect ratio 
set(gcf, 'position', [10 fpos(2) fwidth fheight]) 

% setup all axes 
buf = .15/cols; % buffer between axes & between left edge of figure and axes 
awidth = (1-buf*cols-.08/cols)/cols; % width of all axes 
aidx = 1; 
rowidx = 0; 
while aidx <= length(y) 
    for i = 0:cols-1 
     if aidx+i <= length(y) 
      start = buf + buf*i + awidth*i; 
      apos{aidx+i} = [start 1-rowidx-.92 awidth .85]; 
      a{aidx+i} = axes('position', apos{aidx+i}); 
     end 
    end 
    rowidx = rowidx + 1; % increment row 
    aidx = aidx + cols; % increment index of axes 
end 

% make plots 
axes(a{1}), plot(x,y{1}), title('sine'), xlabel('x'), ylabel('sin(x)') 
axes(a{2}), plot(x,y{2}), title('cosine'), xlabel('x'), ylabel('cos(x)') 
axes(a{3}), plot(x,y{3}), title('tangent'), xlabel('x'), ylabel('tan(x)') 
axes(a{4}), plot(x,y{4}), title('x^2'), xlabel('x'), ylabel('x^2') 
axes(a{5}), plot(x,y{5}), title('x^3'), xlabel('x'), ylabel('x^3') 

% determine the position of the scrollbar & its limits 
swidth = max([.03/cols, 16/scrnsz(3)]); 
ypos = [1-swidth 0 swidth 1]; 
ymax = 0; 
ymin = -1*(rows-1); 

% build the callback that will be executed on scrolling 
clbk = ''; 
for i = 1:length(a) 
    line = ['set(',num2str(a{i},'%.13f'),',''position'',[', ... 
      num2str(apos{i}(1)),' ',num2str(apos{i}(2)),'-get(gcbo,''value'') ', num2str(apos{i}(3)), ... 
      ' ', num2str(apos{i}(4)),'])']; 
    if i ~= length(a) 
     line = [line,',']; 
    end 
    clbk = [clbk,line]; 
end 

% create the slider 
uicontrol('style','slider', ... 
    'units','normalized','position',ypos, ... 
    'callback',clbk,'min',ymin,'max',ymax,'value',0);