【问题标题】:MATLAB adding slider on a figureMATLAB在图形上添加滑块
【发布时间】:2014-01-22 01:36:44
【问题描述】:

我有一个 576x576x150 的矩阵。每个 576x576 集代表一个图像。当我想绘制一帧时,我使用 plot 命令:

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

但是我想在绘图中添加一个滑块,这样我就可以在图中从 1 到 150 帧移动。我见过有人使用 uicontrol 的例子,但我不知道如何编码。除此之外,我想在图的顶部有一个标题,告诉我帧号。

【问题讨论】:

标签: matlab


【解决方案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')));

【讨论】:

  • 我用 load('frames.mat') 替换了第二行,其中包含一个 B(576,576,150) 矩阵,但它不起作用:S
  • 没有错误但是所有的帧都是白色的!!!也许是一个窗口问题。在我使用之前:a=min(min(min(B,[],1)));b=max(max(max(B,[],1))); 然后imshow(B(:,:,frame),[a b]) 但我不知道如何将其添加到您的代码中(如果这是问题的话)。
  • 已确认。这是一个窗口问题。当我将倒数第二行修改为 imshow(vars.B(:,:,get(vars.slider1_handle,'Value')),[]); 时,它可以工作,但我仍然要添加 min = a 和 max = a 例如imshow(vars.B(:,:,get(vars.slider1_handle,'Value')),[a b]); 在所有图像中具有相同的窗口。
  • 啊好吧,我不熟悉imshow。很高兴你解决了。
【解决方案2】:

这个想法是使用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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    相关资源
    最近更新 更多