简体   繁体   中英

How do I embedded another figure within a figure in MatLab?

I'm developing a GUI for my MatLab code using UITab and UITabGroup from this previous post:

How do I create a tabbed GUI in MatLab?

However that code populates each tabs ui within a single function. Since my user interface is going to be more complicated than that, I'm hoping to create a figure for each tab using it's own function .m file and then import that figure into the main GUI function .m file. It attempted to do this by returning the figure from the original function to the main gui:

Tabbed Interface:

function tabbedUI = tab_gui1()
... Code for tabbed UI
end

Main GUI:

function test_embeddedGUI()

    hFig = figure('Menubar', 'none');
    test = tab_gui1(hFig);

    uicontrol('Style', 'pushbutton', 'String', 'This is a simple test', 'Callback', @testButton);

    function testButton(src, evt)

        disp('button was pressed');
    end
end

My problem is that when I create the 'sub figure' it creates a new figure window and doesn't embed it into the main GUI's figure.

:/

How do I create a figure that can be embedded within another figure?

Instead of creating a new a new figure, instead pass the parent object you want to embed the code into. For example, (assuming you used the linked questions' accepted answer's code):

function tab_gui1(parent)
    hTabGroup = uitabgroup('Parent', parent); % parent here is the main GUI figure.
    hTabs(1) = uitab('Parent', hTabGroup, 'Title', 'Data');
    hTabs(2) = uitab('Parent', hTabGroup, 'Title', 'Params');
    hTabs(3) = uitab('Parent', hTabGroup, 'Title', 'Plot');
    set(hTabGroup, 'SelectedTab', hTabs(1));

... Rest of Code is the same

end

Then pass the parent object to the sub GUI function:

function test_embeddedGUI()
    hFig = figure('Menubar', 'none');
    tab_gui1(hFig);  % your parent object being passed is the main figure.

    uicontrol('Style', 'pushbutton', 'String', 'This is a simple test', 'Callback', @testButton);
    function testButton(src, evt)
        disp('button was pressed');
    end
end

However with this particular arrangement you get elements that can/will overlap:

重叠按钮

Notice that the buttons are overlapping. From what I can gather from your question, it appears that you want to have a tabular main interface with sub interfaces for each tab. I suggest creating the tabbed interface on the main GUI and then creating a uipanel for each of those tabs. You would then populate those uipanels using your separate functions. Here's a quick example:

Main UI

function test_embeddedGUI()
    hFig = figure('Menubar', 'none');

    hTabGroup = uitabgroup('Parent', hFig);

    hTabs(1) = uitab('Parent', hTabGroup, 'Title', 'First');
    hTabs(2) = uitab('Parent', hTabGroup, 'Title', 'Second');
    hTabs(3) = uitab('Parent', hTabGroup, 'Title', 'Third');

    set(hTabGroup, 'SelectedTab', hTabs(1));

    firstPanel = uipanel('Title', 'Main Panel', 'Parent', hTabs(1));
    secondPanel = uipanel('Title', 'Secondary Panel', 'Parent', hTabs(2));
    thirdPanel = uipanel('Title', 'Final Panel', 'Parent', hTabs(3));

    subUI1(firstPanel);
end

SubUI:

function subUI1(parent)
    firstButton = uicontrol('Style', 'pushbutton', 'String', 'First Button' ...
        , 'Parent', parent, 'Callback', @buttonPress);

    function buttonPress(src, evt)
        disp('Main Button press');
    end

end

Which will create an interface like so:

好极了!可行的东西!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM