User interface 在GUI的子窗口中添加滚动条

User interface 在GUI的子窗口中添加滚动条,user-interface,matlab,plot,scrollbar,User Interface,Matlab,Plot,Scrollbar,如何将滚动条添加到子绘图 我看过很多教程,但它们似乎很难理解(例如:) 有更容易实现的方法吗 我的代码如下所示。它创建两个图形,一个用于多个绘图,一个用于子绘图。对于子地块的情况,我希望有一个滚动条,以便我可以向下滑动它。 我不知道如何调用此函数,如果我将addaxis的调用替换为我的函数,那么在不调用addaxis函数的情况下如何重新调整轴 function readfile while(1) q = cell(16,2); q{1,1}='1B000003AC63A328.txt'; %

如何将滚动条添加到子绘图

我看过很多教程,但它们似乎很难理解(例如:)

有更容易实现的方法吗



我的代码如下所示。它创建两个图形,一个用于多个绘图,一个用于子绘图。对于子地块的情况,我希望有一个滚动条,以便我可以向下滑动它。 我不知道如何调用此函数,如果我将addaxis的调用替换为我的函数,那么在不调用addaxis函数的情况下如何重新调整轴

function readfile
while(1)

q = cell(16,2);
q{1,1}='1B000003AC63A328.txt'; % sensor 1
q{1,2} = 'sensor 1';
q{2,1}='D6000003ACA0AB28.txt';% sensor 2
q{2,2} = 'sensor 2'; 
q{3,1}='B0000003AC9B8428.txt'; % sensor 3
q{3,2} = 'sensor 3'; 
q{4,1}='5D000003AC5FEA28.txt';% sensor 4
q{4,2} = 'sensor 4'; 
q{5,1}='E1000003AC5DA728.txt';% sensor 5
q{5,2} = 'sensor 5'; 
q{6,1}='BE000003ACA4F828.txt';% sensor 6
q{6,2} = 'sensor 6';
q{7,1}='5F000003AC8C6128.txt';% sensor 7
q{7,2} = 'sensor 7'; 
q{8,1}='78000003AC77A328.txt';
q{8,2} = 'sensor 8'; % sensor 8
q{9,1}='B2000003AC542A28.txt';% sensor 9
q{9,2} = 'sensor 9'; 
q{10,1}='EB000003B717F328.txt';% sensor 10
q{10,2} = 'sensor 10';
q{11,1}='86000003AC97AC28.txt';% sensor 11
q{11,2} = 'sensor 11'; 
q{12,1}='78000003AC748828.txt';% sensor 12
q{12,2} = 'sensor 12'; 
q{13,1}='A5000003AC905C28.txt';% sensor 20
q{13,2} = 'sensor 20'; 
q{14,1}='B4000003ACA4A728.txt';% sensor 21
q{14,2} = 'sensor 21'; 
q{15,1}='14000003AC69A528.txt';% sensor 22
q{15,2} = 'sensor 22'; 
q{16,1}='99000003AC68F728.txt';% sensor 23
q{16,2} = 'sensor 23';

for j=1:16
fname=q{j};
fid=fopen(fname,'r');
header=fgetl(fid);
data=textscan(fid,'%s','delimiter',';');
fclose(fid);
data=data{:};
day=data(1:3:end);
hour=data(2:3:end);
temp=str2double(data(3:3:end));
time=cellfun(@(x) sprintf('%s %s',day{strcmpi(hour,x)},x),hour,'uniformoutput',0);
% timev=datevec(time,'mm.dd.yyyy HH:MM:SS');
timen=datenum(time,'mm.dd.yyyy HH:MM:SS');
seconds=timen*86400/60;
figure(1)
subplot(5,4,j),

h=plot(seconds-seconds(1),temp,'YDataSource','temp');
legend(h,q{j,2});

grid on
xlabel('Time(mins)');
ylabel('Temp °C');

%subplot(1,1,i),
figure(2)
if(j==1)
r=plot(seconds-seconds(1),temp);
hold on
set(r,'Color','blue','LineWidth',2)
end
if(j==2)
    r=plot(seconds-seconds(1),temp);

    set(r,'Color','green','LineWidth',2)
end
if(j==3)
    r=plot(seconds-seconds(1),temp);

    set(r,'Color','red','LineWidth',2)

end
if(j==4)
    r=plot(seconds-seconds(1),temp);

    set(r,'Color','cyan','LineWidth',2)

end

if(j==5)
     r=plot(seconds-seconds(1),temp);

    set(r,'Color','magenta','LineWidth',2)

end
if(j==6)
     r=plot(seconds-seconds(1),temp);

    set(r,'Color','yellow','LineWidth',2)

end
   if(j==7)
     r=plot(seconds-seconds(1),temp);

    set(r,'Color','black','LineWidth',2)

   end
   if(j==8)
     r=plot(seconds-seconds(1),temp,'--');

    set(r,'Color','blue','LineWidth',2)

   end
   if(j==9)
     r=plot(seconds-seconds(1),temp,'--');

     set(r,'color','green','LineWidth',2)

   end
   if(j==10)
     r=plot(seconds-seconds(1),temp,'--');

    set(r,'Color','red','LineWidth',2)

   end
   if(j==11)
     r=plot(seconds-seconds(1),temp,'--');
    set(r,'Color','cyan','LineWidth',2)

   end
   if(j==12)
     r=plot(seconds-seconds(1),temp,'--');
     hold on
    set(r,'Color','magenta','LineWidth',2)

   end
   if(j==13)
     r=plot(seconds-seconds(1),temp,'--');

    set(r,'Color','yellow','LineWidth',2)

   end
   if(j==14)
     r=plot(seconds-seconds(1),temp,'--');

    set(r,'Color','black','LineWidth',2)

   end
   if(j==15)
     r=plot(seconds-seconds(1),temp,'-.');

    set(r,'Color','blue','LineWidth',2)

   end
   if(j==16)
     r=plot(seconds-seconds(1),temp,'-.');
    set(r,'Color','green','LineWidth',2)


   end

legend('Sensor 1','Sensor 2','Sensor 3','Sensor 4','Sensor 5','Sensor 6',...
   'Sensor 7','Sensor 8','Sensor 9','Sensor 10','Sensor 11','Sensor 12','Sensor 20','Sensor 21','Sensor 22','Sensor 23','Location','BestOutside')


end
pause(2*60)
end end

我不确定您指的是什么样的演示,但让我解释一下如何实现这样的功能

其想法是在图形中创建一个大面板,其中包含所有子地块。这个嵌板的尺寸会比这个数字大。您必须手动将轴定位在此面板内。同样使用滑块,您必须保持面板本身的位置,以控制其哪个部分可见

考虑下面的例子。我们将创建一个图形,以便垂直滚动以查看所有子地块

我们首先创建一个图形,然后放置一个面板和一个滑块组件来填充整个图形:

%# create figure, panel, and slider
w = 600; h = 500;           %# width/height of figure
handles.hFig = figure('Menubar','figure', 'Resize','off', ...
    'Units','pixels', 'Position',[200 200 w h]);
handles.hPan = uipanel('Parent',handles.hFig, ...
    'Units','pixels', 'Position',[0 0 w-20 h]);
handles.hSld = uicontrol('Parent',handles.hFig, ...
    'Style','slider', 'Enable','off', ...
    'Units','pixels', 'Position',[w-20 0 20 h], ...
    'Min',0-eps, 'Max',0, 'Value',0, ...
    'Callback',{@onSlide,handles.hPan});
目前,滑块已禁用。请注意,为了保持简单,我关闭了图形大小调整。这样我们就可以以固定的像素单位定位组件

接下来,我们将一次创建一个新轴,使每个轴填充一个视图页面。为了便于使用,我将该代码放在一个单独的函数
addAxis
中。首先,让我演示如何调用此函数:

%# add and plot to axes one-by-one
hAx = zeros(7,1);
clr = lines(7);
for i=1:7
    hAx(i) = addAxis(handles);
    plot(hAx(i), cumsum(rand(100,1)-0.5), 'LineWidth',2, 'Color',clr(i,:))
    title(hAx(i), sprintf('plot %d',i))
    pause(1)   %# slow down so that we can see the updates
end
addAxis
只需增大容器面板的大小,创建一个轴,将其放置在顶部,调整滑块限制,然后将控制柄返回到新创建的轴

function hAx = addAxis(handles)
    %# look for previous axes
    ax = findobj(handles.hPan, 'type','axes');

    if isempty(ax)
        %# create first axis
        hAx = axes('Parent',handles.hPan, ...
            'Units','normalized', 'Position',[0.13 0.11 0.775 0.815]);
        set(hAx, 'Units','pixels');

    else
        %# get height of figure
        p = get(handles.hFig, 'Position');
        h = p(4);

        %# increase panel height, and shift it to show new space
        p = get(handles.hPan, 'Position');
        set(handles.hPan, 'Position',[p(1) p(2)-h p(3) p(4)+h])

        %# compute position of new axis: append on top (y-shifted)
        p = get(ax, 'Position');
        if iscell(p), p = cell2mat(p); end
        p = [p(1,1) max(p(:,2))+h p(1,3) p(1,4)];

        %# create the new axis
        hAx = axes('Parent',handles.hPan, ...
            'Units','pixels', 'Position',p);

        %# adjust slider, and call its callback function
        mx = get(handles.hSld, 'Max');
        set(handles.hSld, 'Max',mx+h, 'Min',0, 'Enable','on')
        %#set(handles.hSld, 'Value',mx+h)       %# scroll to new space
        hgfeval(get(handles.hSld,'Callback'), handles.hSld, []);
    end

    %# force GUI update
    drawnow
end
滑块回调函数仅根据滑块的当前值上下移动面板:

function onSlide(hSld,ev,hPan)
    %# slider value
    offset = get(hSld,'Value');

    %# update panel position
    p = get(hPan, 'Position');  %# panel current position
    set(hPan, 'Position',[p(1) -offset p(3) p(4)])
end
本例的结果如下:


类似的问题:太好了……这真的解决了我的问题……我正在研究这段代码,以便我可以根据自己的要求修改。我必须绘制不同文本文件中的数据,因此我将它们循环到for loop下。我使用了subplot命令,因为我以前没有空间,但现在通过添加滚动条,在hax plot命令中添加了另一个for循环。通过添加所有这些,我的代码只绘制(仅图例)第一个图形,然后是轴,非常随意,没有显示任何图形。我打算自动缩放,不想限制xmin和xmax@Khalid:我不知道您现有的代码是什么,但我认为,在使用此代码之后,您所要做的就是将对
子地块的调用替换为对上述函数的调用
addAxis
(当然,使用正确的参数)它会创建两个图形,一个用于多个地块,一个用于子地块。对于子地块的情况,我希望有一个滚动条,以便我可以向下滑动它。我不知道如何调用此函数,如果我将addaxis的调用替换为我的函数,那么在不调用addaxis函数的情况下如何重新调整轴。这就是我的函数看起来像函数abc子图(5,4,j),h=plot(秒(1),temp,'YDataSource','temp');图例(h,q{j,2});图(2)r=绘图(秒(1),温度);%同一图形上的多个绘图保持Hello!我已经尝试过这个解决方案,它非常棒,但是我对滚动条的位置有问题,我的意思是,这个代码的原始位置在滚动条的底部,但是我需要它在顶部,我希望你能帮助我,顺便说一句,谢谢!我找到了答案,它只是:set(handles.hSld,'Value',get(handles.hSld,'Max'))