DaveSwans
DaveSwans

Reputation: 57

Matlab App Designer. Using DropDown to open and close panels

I'm new to matlab and I was wondering if anyone could shed some light. I am using Matlab R2017B and App Designer.

My question: I have a panel in a GUI that is default set to invisible. I also have a dropdown menu with a few values stored:

"Please Select" "Panel 1" "panel 2" "panel 3" etc...

when i select panel 1 from the dropdown I want that specific panel to then become visible. ie the callback:

function DropDownValueChanged(app, event) app.Panel1.Visible = 'on';

this is all fine and works but I cant seem to get the next part right. When i go back to the dropdown and select "panel 2" What i would like is for the program to then close the current panel and open the panel named "panel 2" by selecting that value from the dropdown.

Am I wrong in using visibility to define this? How can i connect the dropdown values to corresponding panels in a more intuitive way? I've been messing around with all sorts of tutorials but still cant get it to work

Thanks in advance

Upvotes: 0

Views: 2445

Answers (1)

scotty3785
scotty3785

Reputation: 7006

You need to read the value of the dropdown and use that to evaluate which panel should be displayed. Initially hide all the panels and then just make the panel of interest visible. My DropdownValueChanged function looks like this.

    % Value changed function: DropDown
    function DropDownValueChanged(app, event)
        value = app.DropDown.Value;
        % Hide all the panels
        app.Panel1.Visible = 'off';
        app.Panel2.Visible = 'off';
        app.Panel3.Visible = 'off';

        %If Panel 1 is selected, show panel 1
        if strcmp(value,'Panel 1')
            app.Panel1.Visible = 'on';
        elseif strcmp(value,'Panel 2')
            app.Panel2.Visible = 'on';
        elseif strcmp(value,'Panel 3')
            app.Panel3.Visible = 'on';
        end
    end

I also have the following in the startupFcn to hide all but the first panel.

    % Code that executes after component creation
    function startupFcn(app)
        app.Panel1.Visible = 'on';
        app.Panel2.Visible = 'off';
        app.Panel3.Visible = 'off';
    end

Upvotes: 1

Related Questions