WICHTIG: Der Betrieb von goMatlab.de wird privat finanziert fortgesetzt. - Mehr Infos...

Mein MATLAB Forum - goMatlab.de

Mein MATLAB Forum

 
Gast > Registrieren       Autologin?   

Partner:




Forum
      Option
[Erweitert]
  • Diese Seite per Mail weiterempfehlen
     


Gehe zu:  
Neues Thema eröffnen Neue Antwort erstellen

Voneinander abhängige Dropdown-Menüs

 

corleone13375
Forum-Anfänger

Forum-Anfänger


Beiträge: 25
Anmeldedatum: 18.09.15
Wohnort: Aachen
Version: R2015b
     Beitrag Verfasst am: 14.06.2017, 11:22     Titel: Voneinander abhängige Dropdown-Menüs
  Antworten mit Zitat      
Hallo,

ich möchte in einem GUI ein Dropdown-Menü erstellen, welches seinen Inhalt, je nach Auswahl eines anderen Dropdown-Menüs ändert.

Also zB so:
Dropdown-Menü 1 soll die Auswahl A und B bieten.
Wenn A gewählt wurde, dann soll in Dropdown-Menü 2 die Auswahl C und D stehen. Für den Fall, dass in Menü 1 B gewählt wurde, soll in Menü 2 E und F stehen.

Muss dafür in der Opening Function schon eine switch- oder if-Abfrage implementiert werden oder kann ich das in der Callback Function von Dropdown-Menü 2 machen? Oder müssen die Menüs schon in der Figure in GUIDE abhängig gemacht werden?
Private Nachricht senden Benutzer-Profile anzeigen


Jan S
Moderator

Moderator


Beiträge: 11.057
Anmeldedatum: 08.07.10
Wohnort: Heidelberg
Version: 2009a, 2016b
     Beitrag Verfasst am: 20.06.2017, 11:47     Titel: Re: Voneinander abhängige Dropdown-Menüs
  Antworten mit Zitat      
Hallo corleone13375,

Probiere es einfach aus. In der OpeningFcn müsste die Einstellung für die Auswahl getroffen werden, die beim Start aktiviert sein soll - bei Bedarf. Aber es ist auch denkbar, dass das zweite Menü erst erstellt wird, wenn man im ersten die Auswahl getroffen hat.

Zitat:
Muss dafür in der Opening Function schon eine switch- oder if-Abfrage implementiert werden

Das ist recht allgemein.

Zitat:
Oder müssen die Menüs schon in der Figure in GUIDE abhängig gemacht werden?

Es gibt kein Feld oder Flag, um Abhängigkeiten zu aktivieren. Man kann die Änderungen einfach im Callback als Code implementieren und benötigt keine magischen Verknüpfungen.

Gruß, Jan
Private Nachricht senden Benutzer-Profile anzeigen
 
corleone13375
Themenstarter

Forum-Anfänger

Forum-Anfänger


Beiträge: 25
Anmeldedatum: 18.09.15
Wohnort: Aachen
Version: R2015b
     Beitrag Verfasst am: 22.06.2017, 14:29     Titel:
  Antworten mit Zitat      
Irgendwie weiß ich da nicht weiter.
Ich habe zwei Dropdown-Menüs erstellt. Das erste Menu "Auswahl" hat auch richtiger Weise die Einträge "WL1" und "WL2"
Bei der Auswahl von "WL1" soll "Auswahl2" die Optionen "IR1" und "AR1" bieten, was auch noch funktioniert.
Bei einer Wahl von "WL2" soll "Auswahl2" allerdings die Optionen "IR2" und "AR2" anbieten und das funktioniert nicht.
Ich habe versucht in "function Auswahl2_CreateFcn" eine if-Auswahl in Abhängigkeit des gewählten Eintrags in "Auswahl" zu implementieren (nun auskommentiert), aber das funktioniert nicht.
Ich bezweifel auch, dass das an der Stelle der richtige Ansatz ist.

Code:
function varargout = DropDownMenu(varargin)
% DropDownMenu MATLAB code for DropDownMenu.fig
%      DropDownMenu, by itself, creates a new DropDownMenu or raises the existing
%      singleton*.
%
%      H = DropDownMenu returns the handle to a new DropDownMenu or the handle to
%      the existing singleton*.
%
%      DropDownMenu('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in DropDownMenu.M with the given input arguments.
%
%      DropDownMenu('Property','Value',...) creates a new DropDownMenu or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before DropDownMenu_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to DropDownMenu_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help DropDownMenu

% Last Modified by GUIDE v2.5

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @DropDownMenu_OpeningFcn, ...
                   'gui_OutputFcn',  @DropDownMenu_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before DropDownMenu is made visible.
function DropDownMenu_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to DropDownMenu (see VARARGIN)

% Choose default command line output for DropDownMenu
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

if ~isfield(handles,'initialized')
    % Flag not present, so create and store it
    % this allows for 1st instance variable initialization
    handles.initialized = true;
   % the code below sets the menu up
    x = {  'WL1';
           'WL2'};
   
    set(handles.Auswahl, 'String', x);
    handles.test_data = -1; % this means that no relevant option has been chosen

end % end if
%end % end function call

% Plot Initialisierung
    grid
    xlabel('X');
    ylabel('Y');
%end

% UIWAIT makes DropDownMenu wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = DropDownMenu_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on selection change in Auswahl.
function Auswahl_Callback(hObject, eventdata, handles)
% hObject    handle to Auswahl (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) % returns Auswahl contents as cell array
%        contents = {get(hObject,'Value')}; % returns selected item from Auswahl



% --- Executes during object creation, after setting all properties.
function Auswahl_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Auswahl (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in Auswahl2.
function Auswahl2_Callback(hObject, eventdata, handles)
% hObject    handle to Auswahl2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns Auswahl2 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from Auswahl2


% --- Executes during object creation, after setting all properties.
function Auswahl2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Auswahl2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
% Plot Auswahl
% if x == WL1
    set(hObject,'String',{'AR1','IR1'});
% elseif x == WL2
   % set(hObject,'String',{'AR2','IR2'});
% end


% --- Executes on button press in plotbutton1.
function plotbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to plotbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

axes(handles.axes1); % Plot Fenster erzeugen
cla; % Plotinhalt loeschen

St=get(handles.Auswahl2,'String'); %Plot Auswahl holen (String fuer Titel)
popup_sel_index=get(handles.Auswahl2,'Value'); %Welcher Plot wurde ausgewaehlt

switch popup_sel_index
    case 1
        AR1_2D_Profil
        plot(x,y)
        grid
        title(St(1));
    case 2
        IR1_2D_Profil
        plot(x,y)
        grid
        title(St(2));
end
 
Private Nachricht senden Benutzer-Profile anzeigen
 
Jan S
Moderator

Moderator


Beiträge: 11.057
Anmeldedatum: 08.07.10
Wohnort: Heidelberg
Version: 2009a, 2016b
     Beitrag Verfasst am: 24.06.2017, 13:53     Titel:
  Antworten mit Zitat      
Hallo corleone13375,

Code:
function DropDownMenu_OpeningFcn(hObject, eventdata, handles, varargin)
% Choose default command line output for DropDownMenu
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);   % <-- Nich sinnvoll hier!

if ~isfield(handles,'initialized')
    % Flag not present, so create and store it
    % this allows for 1st instance variable initialization
    handles.initialized = true;
   % the code below sets the menu up
    x = {  'WL1';
           'WL2'};
   
    set(handles.Auswahl, 'String', x);
    handles.test_data = -1; % this means that no relevant option has been chosen
end % end if

% Jetzt ist handles geändert, aber noch nicht im GUI gespeichert.
% Also besser hier:
% Update handles structure
guidata(hObject, handles);   % <-- Nich sinnvoll hier!
...
end

% ??? DropDown menüs haben keinen Output:
% --- Outputs from this function are returned to the command line.
function varargout = DropDownMenu_OutputFcn(hObject, eventdata, handles)

% Get default command line output from handles structure
varargout{1} = handles.output;
????

% --- Executes on selection change in Auswahl.
function Auswahl_Callback(hObject, eventdata, handles)

% Genau hier möchtest Du den Inhalt des zweiten DropDown-Menüs ändern:
String = get(hObject, 'String');
Value = get(hObject, 'Value');
switch String{Value}
  case 'WL1'
    set(handles.Auswahl2, 'String', {'Keine', 'Ahnung', '1'});
  case 'WL2'
    set(handles.Auswahl2, 'String', {'Weiß', 'nich', '2'});
  otherwise  % No SWITCH without OTHERWISE!!!!
    error('Unexpected switch expression')
end


Klarer?

Gruß, Jan
Private Nachricht senden Benutzer-Profile anzeigen
 
corleone13375
Themenstarter

Forum-Anfänger

Forum-Anfänger


Beiträge: 25
Anmeldedatum: 18.09.15
Wohnort: Aachen
Version: R2015b
     Beitrag Verfasst am: 26.06.2017, 09:36     Titel:
  Antworten mit Zitat      
Danke Jan! Das hat mir weitergeholfen.

Die update Funktion kann ich leider nicht nutzen.
Code:
To use 'Update', you might need:
  Update - Simulink
  Update - Model-Based Calibration Toolbox

Error in DropDownMenu2>DropDownMenu2_OpeningFcn (line 74)
Update handles structure

Es klappt aber auch ohne die Zeile.

Wieso haben DropDown Menüs keine Output Function? Ich hab da zwar nichts rein geschrieben, aber die Funktion wurde von Matlab nach erstellen des GUI automatisch erzeugt. Wenn ich die Outpu Function lösche, erhalte ich einen Fehler. Daher habe ich sie drin gelassen.


Mein Programm sieht jetzt so aus und es funktioniert. Bei Auswahl von WL1 kann AR1 und IR1 gewählt werden und bei Wahl von WL2 zwischen AR2 und IR2.

Code:
function varargout = DropDownMenu2(varargin)
% DROPDOWNMENU2 MATLAB code for DropDownMenu2.fig
...
% End initialization code - DO NOT EDIT


% --- Executes just before DropDownMenu2 is made visible.
function DropDownMenu2_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to DropDownMenu2 (see VARARGIN)

% Choose default command line output for DropDownMenu2
handles.output = hObject;

if ~isfield(handles,'initialized')
    % Flag not present, so create and store it
    % this allows for 1st instance variable initialization
    handles.initialized = true;
   % the code below sets the menu up
    x = {  'WL1';
           'WL2'};  
    set(handles.Auswahl, 'String', x);
    handles.test_data = -1; % this means that no relevant option has been chosen
end

% Plot Initialisierung
   grid
   xlabel('X');
   ylabel('Y');

% UIWAIT makes DropDownMenu2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);

function varargout = DropDownMenu2_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
%varargout{1} = handles.output;

% --- Executes on selection change in Auswahl.
function Auswahl_Callback(hObject, eventdata, handles)
% hObject    handle to Auswahl (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) % returns Auswahl contents as cell array
%        contents = {get(hObject,'Value')}; % returns selected item from Auswahl

String = get(hObject, 'String');
Value = get(hObject, 'Value');
switch String{Value}
  case 'WL1'
    set(handles.Auswahl2, 'String', {'AR1', 'IR1',});
  case 'WL2'
    set(handles.Auswahl2, 'String', {'AR2', 'IR2',});
  otherwise
    error('Unexpected switch expression')
end

% --- Executes during object creation, after setting all properties.
function Auswahl_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Auswahl (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes on selection change in Auswahl2.
function Auswahl2_Callback(hObject, eventdata, handles)
% hObject    handle to Auswahl2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns Auswahl2 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from Auswahl2

% --- Executes during object creation, after setting all properties.
function Auswahl2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Auswahl2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
    set(hObject,'String',{'AR1','IR1'});

% --- Executes on button press in plotbutton1.
function plotbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to plotbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

axes(handles.axes1); % Plot Fenster erzeugen
cla; % Plotinhalt loeschen

St=get(handles.Auswahl2,'String'); % Plot Auswahl holen (String fuer Titel)
popup_sel_index=get(handles.Auswahl2,'Value'); % Was wurde in Menu2 ausgewaehlt
Menu1=get(handles.Auswahl,'Value'); % Was wurde in Menu1 ausgewaehlt

switch Menu1
    case 1
        switch popup_sel_index
            case 1
                AR1_2D_Profil
                plot(x,y)
                xlabel('X');
                ylabel('Y');
                grid
                title(St(1));
            case 2
                IR1_2D_Profil
                plot(x,y)
                xlabel('X');
                ylabel('Y');
                grid
                title(St(2));
            otherwise
                error('Unexpected switch expression')
        end
   case 2
       switch popup_sel_index
            case 1
                AR2A_2D_Profil
                plot(x,y)
                xlabel('X');
                ylabel('Y');
                grid
                title(St(1));
            case 2
                IR2A_2D_Profil
                plot(x,y)
                xlabel('X');
                ylabel('Y');
                grid
                title(St(2));
            otherwise
                error('Unexpected switch expression')    
       end
end
 
Private Nachricht senden Benutzer-Profile anzeigen
 
Jan S
Moderator

Moderator


Beiträge: 11.057
Anmeldedatum: 08.07.10
Wohnort: Heidelberg
Version: 2009a, 2016b
     Beitrag Verfasst am: 26.06.2017, 11:26     Titel:
  Antworten mit Zitat      
Hallo corleone13375,

"% Update handles structure" war ein Kommentar, kein Code.

Zitat:
Wieso haben DropDown Menüs keine Output Function?

Ein Dropdown-Menü ist ein GUI-Element, bei dem man einen Eintrag aus einer Liste Auswählen kann. Es ist nicht sinnvoll von dort irgendwohin etwas auszugeben, weil nicht eindeutig klar ist, wohin das sein sollte. Ein "Output" ist entweder die Rückgabe eine Variable an den Caller (aber ein Callback ist eine Reaktion auf eine User-Aktivität, also gibt es keinen Caller) oder eine Ausgabe ins CommandWindows (also in den BaseWorkspace), aber dort wird gar nichts erwartet.

Zitat:
Wenn ich die Outpu Function lösche, erhalte ich einen Fehler.

Ah, kann es sein, dass das GUI "DropDownMenu" heißt? Dann wäre dies die OutputFcn des GUIs, aber nicht eines dropdown-Menüs.

Gruß, Jan
Private Nachricht senden Benutzer-Profile anzeigen
 
corleone13375
Themenstarter

Forum-Anfänger

Forum-Anfänger


Beiträge: 25
Anmeldedatum: 18.09.15
Wohnort: Aachen
Version: R2015b
     Beitrag Verfasst am: 26.06.2017, 12:12     Titel:
  Antworten mit Zitat      
Zitat:

Zitat:
Wenn ich die Outpu Function lösche, erhalte ich einen Fehler.

Ah, kann es sein, dass das GUI "DropDownMenu" heißt? Dann wäre dies die OutputFcn des GUIs, aber nicht eines dropdown-Menüs.

Ja genau. Das GUI heißt DropDownMenu und die Dropdowns selber heißen Auswahl und Auswahl2.
Hatte das zum Testen mal seperat geschrieben und werde das jetzt auf meine Arbeit übertragen.
Private Nachricht senden Benutzer-Profile anzeigen
 
Neues Thema eröffnen Neue Antwort erstellen



Einstellungen und Berechtigungen
Beiträge der letzten Zeit anzeigen:

Du kannst Beiträge in dieses Forum schreiben.
Du kannst auf Beiträge in diesem Forum antworten.
Du kannst deine Beiträge in diesem Forum nicht bearbeiten.
Du kannst deine Beiträge in diesem Forum nicht löschen.
Du kannst an Umfragen in diesem Forum nicht mitmachen.
Du kannst Dateien in diesem Forum posten
Du kannst Dateien in diesem Forum herunterladen
.





 Impressum  | Nutzungsbedingungen  | Datenschutz | FAQ | goMatlab RSS Button RSS

Hosted by:


Copyright © 2007 - 2024 goMatlab.de | Dies ist keine offizielle Website der Firma The Mathworks

MATLAB, Simulink, Stateflow, Handle Graphics, Real-Time Workshop, SimBiology, SimHydraulics, SimEvents, and xPC TargetBox are registered trademarks and The MathWorks, the L-shaped membrane logo, and Embedded MATLAB are trademarks of The MathWorks, Inc.