如何在Matlab中创建一个包含多个图形(不是子图或保持)的窗口

时间:2015-08-20 17:47:20

标签: matlab matlab-figure

我正在使用GUI。在其中一个轴上,我想绘制多个图形,并且能够一次点击一个。每个图有2个子图(见图像链接)。

http://www.edwinlengzai.com

是否存在使用下一个/上一个按钮在单个窗口中绘制多个图形的现有代码,或者在GUIDE中的轴或面板内执行此操作的方法?我不能只是创造' x'面板数量和切换可见性,因为绘制的数字数量取决于正在处理的数据大小。

1 个答案:

答案 0 :(得分:1)

这是一种探索明确问题答案的程序化方法。此函数“加载”一组虚拟数据,并根据加载的变量数生成N个面板。在这种情况下,它是我创建的数据结构的长度。您可以使用按钮在面板之间导航。

function testcode()
% Initialize GUI
h.myfig = figure;

h.loadbutton = uicontrol( ...
    'Parent', h.myfig, ...
    'Style', 'Pushbutton', ...
    'Units', 'Normalized', ...
    'Position', [0.015 0.75 0.3 0.2], ...
    'String', 'Load Data', ...
    'Callback', {@loaddata} ...
    );

h.previousplotbutton = uicontrol( ...
    'Parent', h.myfig, ...
    'Style', 'Pushbutton', ...
    'Units', 'Normalized', ...
    'Position', [0.015 0.35 0.3 0.2], ...
    'String', 'Previous Plot', ...
    'Tag', 'previous', ...
    'Callback', {@switchpanel} ...
    );

h.nextplotbutton = uicontrol( ...
    'Parent', h.myfig, ...
    'Style', 'Pushbutton', ...
    'Units', 'Normalized', ...
    'Position', [0.015 0.10 0.3 0.2], ...
    'String', 'Next Plot', ...
    'Tag', 'next', ...
    'Callback', {@switchpanel} ...
    );

h.plotpanel = uipanel( ...
    'Parent', h.myfig, ...
    'Units', 'Normalized', ...
    'Position', [0.33 0 0.66 1], ...
    'Title', 'Plotting Panel' ...
    );

guidata(h.myfig, h);

function loaddata(hObj, ~)
h = guidata(hObj);

% Make some dummy data
for ii = 1:5
    mydata(ii).x = 1:10;
    mydata(ii).y = rand(10, 1);
end

nvariables = length(mydata);
h.plotsubpanels = genpanels(h.plotpanel, nvariables, mydata);

guidata(hObj, h);

function h = genpanels(parentobj, N, mydata)
% Generate array of panels and axes objects

for ii = 1:N
    h.panel(ii) = uipanel( ...
        'Parent', parentobj, ...
        'BorderWidth', 0, ...
        'BorderType', 'none', ...
        'Title', sprintf('Panel %u', ii), ...
        'TitlePosition', 'centertop', ...
        'Tag', sprintf('%u', ii), ...
        'Visible', 'off' ...
        );
    h.topax(ii) = subplot(2, 1, 1, 'Parent', h.panel(ii));
    h.botax(ii) = subplot(2, 1, 2, 'Parent', h.panel(ii));

    plot(h.topax(ii), mydata(ii).y, mydata(ii).x);
    plot(h.botax(ii), mydata(ii).x, mydata(ii).y);
end

h.panel(1).Visible = 'on';

function switchpanel(hObj, eventdata)
h = guidata(hObj);
temp = {h.plotsubpanels.panel.Visible}; % Pull panel visibility
currentpanel = find(strcmp(temp, 'on'), 1); % Find the active panel
npanels = length(temp); % Find number of panels

switch lower(eventdata.Source.Tag) % Figure out which button was pushed
    case 'next'
        if currentpanel < npanels % Make sure we're not at the end
            h.plotsubpanels.panel(currentpanel).Visible = 'off';
            h.plotsubpanels.panel(currentpanel + 1).Visible = 'on';
        end
    case 'previous'
        if currentpanel > 1 % Make sure we're not at the beginning
            h.plotsubpanels.panel(currentpanel).Visible = 'off';
            h.plotsubpanels.panel(currentpanel - 1).Visible = 'on';
        end
end
guidata(hObj, h);

你的GUIDE gui中唯一没有的重要补充是genpanels函数,但添加和调用会很简单。您只需要确保使用方法(例如guidatagetappdata / setappdata)来传递GUI周围的句柄,以便可以访问子面板/轴。晚点。希望这会有所帮助。