Skip to content
Snippets Groups Projects
mgui.m 54.8 KiB
Newer Older
marwan's avatar
marwan committed
function mgui(action)
marwan's avatar
marwan committed
% MGUI   GUI for data analysis programmes.
%    MGUI starts a GUI and supplies Matlab programmes
%    for their easy application to data which are in 
%    the Matlab workspace. 
%
%    The presented programmes are stored in the file
%    MGUI.RC where own programmes can be added. Just
%    include a line with the name of the programme and
%    the minimal and maximal number of arguments, divided
%    by a blank space or tabulator as a separator, e.g.
%
%       plot 1 4
%
%    If the embedded programmes provide an output, then 
%    this output will be stored in the variable ANS in
%    the Matlab workspace.
%
%    See also MGUICLEAN.

marwan's avatar
marwan committed
% Copyright (c) 2008-2009
% Norbert Marwan, Potsdam Institute for Climate Impact Research, Germany
% http://www.pik-potsdam.de
%
% Copyright (c) 2002-2008
% Christian Hoennicke, Norbert Marwan, Andre Sitz, Potsdam University, Germany
% http://www.agnld.uni-potsdam.de
marwan's avatar
marwan committed
%
marwan's avatar
marwan committed
% All rights reserved.
% 
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions
% are met:
% 
%     * Redistributions of source code must retain the above
%       copyright notice, this list of conditions and the following
%       disclaimer.
%     * Redistributions in binary form must reproduce the above
%       copyright notice, this list of conditions and the following
%       disclaimer in the documentation and/or other materials provided
%       with the distribution.
%     * All advertising materials mentioning features or use of this 
%       software must display the following acknowledgement:
%       This product includes software developed by the University of
%       Potsdam, Germany, the Potsdam Institute for Climate Impact
%       Research (PIK), and its contributors.
% 
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
% CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
% INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
% MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
% DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
% BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
% EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
% TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
% DATA, OR PROFITS; OR BUSINESS
%
marwan's avatar
marwan committed
% $Date$
% $Revision$
%
% $Log$
marwan's avatar
marwan committed
% Revision 1.13  2009/03/24 08:32:57  marwan
% copyright address changed
%
marwan's avatar
marwan committed
% Revision 1.12  2006/03/29 13:07:55  marwan
% problems regarding OPRPs and embedding resolved
%
% Revision 1.11  2006/03/16 14:55:57  marwan
% code flattened
%
marwan's avatar
marwan committed
% Revision 1.10  2006/02/14 11:45:49  marwan
% *** empty log message ***
%
marwan's avatar
marwan committed
% Revision 1.9  2004/11/10 07:05:21  marwan
% initial import
marwan's avatar
marwan committed

warning off

if nargin==0
  action='init';
end

    font.FontName=get(0,'factoryTextFontName');
    font.FontUnits=get(0,'factoryTextFontUnits');
    font.FontSize=get(0,'factoryTextFontSize');
    font.FontWeight=get(0,'factoryTextFontWeight');
    font.FontAngle=get(0,'factoryTextFontAngle');

    props.root.defaultUIControlBackgroundColor=get(0,'factoryUIControlBackgroundColor');
    props.root.defaultUIControlForegroundColor=get(0,'factoryUIControlForegroundColor');
    props.root.defaultTextColor=get(0,'factoryUIControlForegroundColor');
    props.window.Color=[0.8008 0.7500 0.6875];
    props.window.Units='Char';
    props.window.Colormap=[[0 0 0];[props.window.Color]];
    props.newwindow.Color=[.8 .8 .8];
    props.newwindow.Units='Pixels';
    props.frame=font;
    props.frame.Style='frame';    
    props.frame.BackgroundColor=props.window.Color;
    props.frame.ForegroundColor=[0 0 0];
    props.frame.Units='Char';
    props.listbox=font;
    props.listbox.FontName='lucida';
    props.listbox.FontSize=10;
    props.listbox.Style='listbox';    
    props.listbox.BackgroundColor=[0.9297 0.8711 0.7969];
    props.listbox.ForegroundColor=[0 0 0];
    props.listbox.Units='Char';
    props.button=font;
    props.button.Style='pushbutton';    
    props.button.BackgroundColor=[0.8008 0.7500 0.6875];
    props.button.ForegroundColor=[0 0 0];
    props.button.Units='Char';
    props.checkbox=font;
    props.checkbox.Style='checkbox';
    props.checkbox.HorizontalAlignment='left';
    props.checkbox.BackgroundColor=props.window.Color;
    props.checkbox.ForegroundColor=[0 0 0];
    props.checkbox.Units='Char';
    props.text=font;
    props.text.Style='text';
    props.text.HorizontalAlignment='left';
    props.text.BackgroundColor=props.window.Color;
    props.text.ForegroundColor=[0 0 0];
    props.text.Units='Char';
    props.msgbox=font;
    props.msgbox.FontSize=get(0,'factoryUIControlFontSize');
    props.msgbox.BackgroundColor=props.window.Color;
    props.msgbox.ForegroundColor=[0 0 0];
    props.msgboxwin.Color=props.msgbox.BackgroundColor;
    v=version;
marwan's avatar
marwan committed
    if str2double(v(findstr(v,'(R')+2:findstr(v,')')-1))<= 12;
       props.msgboxwin.Colormap=[[0 0 0];[props.msgbox.BackgroundColor]];
marwan's avatar
marwan committed
    end
    props.logo.Visible='off';
    props.logo.Units='Char';
    props.logo.Color=props.window.Color;

    set(0,'Units','Character'), sc=get(0,'ScreenSize'); 
    set(0,'Units','Pixels'), sc2=get(0,'ScreenSize');
    po=get(0,'defaultFigurePosition');
    props.window.Position=[4 sc(4)-32 125.1667 29.0714]; % Mainframe
    props.newwindow.Position=[sc2(3)-1.05*po(3) po(2) po(3) po(4)]; % Ausgabefenster
    props.frame.Position=[28.0000  5.5 94.8333 21.67]; % frame um die parameterliste
    props.listbox_function_select.Position=[  3.1667  5.5 23.5000 22.1000]; % Funktionselect [.1 .8 .8 .1]
    props.button_apply.Position=[ 99.8333  1.8571 16.8333  2.1429]; % Knopf 1 :: Ausfhren [.52 .05 .38 .10]
    props.button_parm_le.Position=[ 28.9 24.3  3.5000  1.5000]; % parm_le :: Parameter verringern [.1 .1 .3 .1]
    props.button_parm_ge.Position=[117.9 24.3  3.5000  1.5000]; % parm_ge :: Parameter erhhen [.1 .2 .3 .1]
    props.listbox_parm.Position=[ 34.  7.9000 18.5000 16.5000]; % PopupBox (Listbox)
    props.button_ok.Position=[ 34.  6.4 18.5000  1.5000]; % Knopf schliesen
    props.button_parm.Position=[ 34. 24.3 18.5000  1.5000]; % ParameterButton
    props.button_help.Position=[ 76.5000  1.8571 10.1667  2.1429]; % Knopf 5 :: Help
    props.button_close.Position=[ 88.1667  1.8571 10.1667  2.1429]; % Knopf 6 :: Close
    props.text_disclaimer.Position=[  11.5  0.7 23.5000  3.6429]; % Disclaimer
    props.logo.Position=[  3.1667  0.9 6.8 3.5]; % Disclaimer
    props.checkbox_CheckNewWindow.Position=[  36.1667  1.3571 35.1667  1.5000]; % CheckNewWindow
    props.checkbox_ForceSameLength.Position=[ 36.1667  3.0000 35.1667  1.5000]; % ForceSameLength
    props.text_funktion_helptext.Position=[ 29 26.2857 93.5000  1.2857]; % funktion_helptext
marwan's avatar
marwan committed

marwan's avatar
marwan committed
set(0,'defaultUIControlBackgroundColor',props.msgbox.BackgroundColor,...
      'defaultUIControlForegroundColor',props.msgbox.ForegroundColor,...
      'defaultTextColor',props.text.ForegroundColor)

marwan's avatar
marwan committed
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% splash the BSD
marwan's avatar
marwan committed

filename='mgui';
which_res=which([filename,'.m']);
marwan's avatar
marwan committed
bsdrc_path=[strrep(which_res,[filename,'.m'],''), 'private'];
bsdrc_file=[bsdrc_path, filesep, '.bsd.',filename];
if ~exist(bsdrc_path,'dir')
marwan's avatar
marwan committed
  mkdir(strrep(which_res,[filename,'.m'],''),'private')
end
marwan's avatar
marwan committed
if ~exist(bsdrc_file,'file')
  fid=fopen(bsdrc_file,'w');
  fprintf(fid,'%s\n','If you delete this file, the BSD License will');
marwan's avatar
marwan committed
  fprintf(fid,'%s','splash up at the next time the programme starts.');
  fclose(fid);

marwan's avatar
marwan committed
  if exist('bsd')
    txt=bsd;
marwan's avatar
marwan committed
  else
marwan's avatar
marwan committed
    txt={'The BSD License was not found.'};
marwan's avatar
marwan committed
  end
  h=figure('NumberTitle','off',...,
         'ButtonDownFcn','close',...
marwan's avatar
marwan committed
         'Name','BSD License');
marwan's avatar
marwan committed
  ha=get(h,'Position');
  h=uicontrol('Style','Listbox',...
            'ButtonDownFcn','close',...
            'CallBack','close',...
            'Position',[0 0 ha(3) ha(4)],...
marwan's avatar
marwan committed
            'FontName','Courier',...
            'BackgroundColor',[.8 .8 .8],...
            'String',txt);
marwan's avatar
marwan committed
  waitfor(h)
end


hMF = findobj('Tag','MainFrame');
if ~(isempty(hMF)), props = get(hMF,'UserData'); end
if ~isempty(findobj('Tag','helpdlgbox')), delete(findobj('tag','helpdlgbox')), end
hfs    = findobj('Tag','function_select');
select = get(hfs,'Value');
funktion = get(hfs,'UserData');
if ~isempty(funktion)
  FktName = char(funktion(select).name);
  if strcmp(FktName,'------------');
    select=select+1;
    set(hfs,'Value',select)
    mgui action
  end
end
set(0,'ShowHiddenHandles','off')

% Und "Aktion!" - hier geht es los
switch(action)

  case 'init'

  %%% Hier wird der Programmablauf vorbereitet,
  %%% * die Graphikkoordinaten festgelegt
  %%% * die Funktionslisten aus der Datei generiert
  %%% * das Grundfenster gezeichnet

   % Lsche bestehende Fenster gleichen Tags
    if ~isempty(findobj('Tag','MainFrame'))
      delete(findobj('Tag','MainFrame'))
    end

    % Die Funktionen Load und Save am Anfang einfuegen
    i = 1;
    funktion(i).name = cellstr('load');
    funktion(i).min = 0;
    funktion(i).max = 0;
    funktion(i).akt = 0; 
    funktion(i).klicklist{4}=[];
    funktion(i).klickstring{4}=[];
    i=i+1;

    funktion(i).name = cellstr('save');
    funktion(i).min = 1;
    funktion(i).max = 1;
    funktion(i).akt = 1; 
    funktion(i).klicklist{4}=[];
    funktion(i).klickstring{4}=[];
    i=i+1;

    funktion(i).name = cellstr('add programme');
    funktion(i).min = 0;
    funktion(i).max = 0;
    funktion(i).akt = 0; 
    funktion(i).klicklist{4}=[];
    funktion(i).klickstring{4}=[];
    i=i+1;

    funktion(i).name = cellstr('------------');
    funktion(i).min = 0;
    funktion(i).max = 0;
    funktion(i).akt = 0; 
    funktion(i).klicklist{4}=[];
    funktion(i).klickstring{4}=[];

    %% Einlesen der Funktionsliste
    % ffnen der Datei mit den Funktionensnamen und -parametern
    fid = fopen('mgui.rc','r');
    if (fid ~= -1) % fopen liefert im Fehlerfall "-1"
      line = fgetl(fid); 
      while (line ~=-1) %EOF
        if ~isempty(line) & line(1)~='%' %einlesen erster Parameter
          i = i + 1;
          [name,line] = strtok(line);
          funktion(i).name = cellstr(name);
          [min,line] = strtok(line);
          if ~isempty(min)
marwan's avatar
marwan committed
            funktion(i).min = str2double(min);
            max = strtok(line);
marwan's avatar
marwan committed
            if ~isempty(max) 
              % Hack : mehr als 4 Parameter werden nicht zugelassen
marwan's avatar
marwan committed
              funktion(i).max = str2double(max);
marwan's avatar
marwan committed
              if (funktion(i).max > 4)
                funktion(i).max = 4;
              end
marwan's avatar
marwan committed
              funktion(i).akt = str2double(min);
              funktion(i).klicklist{4}=[];
              funktion(i).klickstring{4}=[];
marwan's avatar
marwan committed
              % Fehlerfall min > max
              if funktion(i).min > funktion(i).max
                h=errordlg([' Error in data structure at: ' char(funktion(i).name) ' -> (min>max)'],'Initialization');
                set(h,props.msgboxwin),h2=guihandles(h);set(h2.OKButton,props.msgbox)
marwan's avatar
marwan committed
                waitfor(h)
marwan's avatar
marwan committed
              end
         % Fehler: "Zu wenige Parameter" einsammeln
            else
              h=errordlg([' Error in data structure at: ' char(funktion(i).name) ' -> (to less parameter)'],'Initialization');
              set(h,props.msgboxwin),h2=guihandles(h);set(h2.OKButton,props.msgbox)
marwan's avatar
marwan committed
              waitfor(h)
marwan's avatar
marwan committed
              funktion(i).min = 0;
              funktion(i).max = 0;
              funktion(i).akt = 0;
marwan's avatar
marwan committed
              funktion(i).klicklist{4}=[];
              funktion(i).klickstring{4}=[];
marwan's avatar
marwan committed
            end
          else
            h=errordlg([' Error in data structure at: ' char(funktion(i).name) ' -> (to less parameter)'],'Initialization'); 
marwan's avatar
marwan committed
            set(h,props.msgboxwin),h2=guihandles(h);set(h2.OKButton,props.msgbox)
marwan's avatar
marwan committed
            waitfor(h)
            funktion(i).min = 0;
            funktion(i).max = 0;
            funktion(i).akt = 0;
marwan's avatar
marwan committed
            funktion(i).klicklist{4}=[];
            funktion(i).klickstring{4}=[];
marwan's avatar
marwan committed
          end
        end
        line = fgetl(fid); % Zeilenweises lesen
      end
      fclose(fid); % schliesen der Datei

    % Wenn fopen() fehlgeschlagen ist
    else
marwan's avatar
marwan committed
      error('File mgui.rc not found.');
marwan's avatar
marwan committed
%       funktion.name = cellstr('Empty');
%       funktion.min = 0;
%       funktion.max = 0;
%       funktion.akt = 0;
%       funktion.klicklist{4}=[];
%       funktion(i).klickstring{4}=[];
    end

    % Schmeie alle unbekannten Funktionen raus...
    j = size(funktion,2); i=5;
    while i<=j
      if isempty(double(which(char(funktion(i).name))))
        funktion(i) = [];
        i = i-1;
        j = j-1;
      end
        i=i+1;
    end

    %% Zeichnen der Obeflche
    % Zeichnen des Grundfensters (Hack: Koordinaten sind fix)
    figure(props.window,'Tag','MainFrame',...
           'NumberTitle','off',...
           'Name','Controlpanel',...
           'MenuBar','None',...
marwan's avatar
marwan committed
           'DeleteFcn','mgui winclose',...
marwan's avatar
marwan committed
           'UserData',props,...
           'Resize','Off');

    uicontrol(props.frame);
    
    if length(funktion)>4, select=5; else select=3; end

    uicontrol(props.listbox, props.listbox_function_select,...
              'Tag','function_select',... 
              'String', [funktion.name]',...
              'UserData', funktion,...
marwan's avatar
marwan committed
              'Value',select,...
marwan's avatar
marwan committed
              'Callback', 'mgui mod2', 'ButtonDownFcn','helpwin mgui');

    % Knopf 3 :: Parameter verringern
b=[fliplr(triu(ones(10,10))),triu(ones(10,10))];
b=rot90(b(:,1:2:end));
for i=1:3,
c=(b==1)*props.button.BackgroundColor(i)+(b==0)*props.button.ForegroundColor(i);
icon(:,:,i)=c(:,1:2:end);
end
    uicontrol(props.button, props.button_parm_le,...
              'Tag','parm_le',...
marwan's avatar
marwan committed
              'CData',icon,...
marwan's avatar
marwan committed
              'Visible', 'off',...
              'Callback','mgui ChangeNumberOfParameters',... 
              'ToolTip','Decreases the number of parameters.');

    for i =1:4,
        uicontrol(props.button, 'Tag',['button_parm' num2str(i)],...
                  'Position', [(props.button_parm.Position(1) + ((i - 1) * 21.2)), props.button_parm.Position(2), props.button_parm.Position(3), props.button_parm.Position(4)],...
                  'String', ['Parm' num2str(i)],...
                  'Visible', 'off',...
                  'Callback', 'mgui Vis');
marwan's avatar
marwan committed
                  
        uicontrol(props.listbox, 'Tag',['listbox_parm' num2str(i)],...
marwan's avatar
marwan committed
                  'Value',[],...
                  'Max',2,...  %Hack: scheinbar beliebig Zahl(>1) mglich...
marwan's avatar
marwan committed
                  'ButtonDownFcn','mgui selectcolumn',...
marwan's avatar
marwan committed
                  'Visible', 'off',...
                  'Position', [(props.listbox_parm.Position(1) + ((i - 1) * 21.2)), props.listbox_parm.Position(2), props.listbox_parm.Position(3), props.listbox_parm.Position(4)],...
                  'Callback','mgui klickreihenfolge'); 
marwan's avatar
marwan committed
                  
marwan's avatar
marwan committed
        uicontrol(props.button, 'Tag',['button_close_parm' num2str(i)],...
                  'String', 'OK',...
                  'Visible', 'off',...
                  'Position', [(props.button_ok.Position(1) + ((i - 1) * 21.2)), props.button_ok.Position(2), props.button_ok.Position(3), props.button_ok.Position(4)],...
                  'Callback', 'mgui UnVis',...
                  'UserData',[]);
    end


    % Knopf 4:: Parameter erhhen
 b=[fliplr(triu(ones(10,10))),triu(ones(10,10))];
 b=rot90(b(:,1:2:end),-1);
 for i=1:3,
 c=(b==1)*props.button.BackgroundColor(i)+(b==0)*props.button.ForegroundColor(i);
 icon(:,:,i)=c(:,1:2:end);
 end

    uicontrol(props.button, props.button_parm_ge,...
              'Tag','parm_ge',...
marwan's avatar
marwan committed
              'CData',icon,...
marwan's avatar
marwan committed
              'Visible', 'on',...
              'Callback','mgui ChangeNumberOfParameters',... 
              'ToolTip','Increases the number of parameters.');

    uicontrol(props.checkbox, props.checkbox_ForceSameLength, ...
              'Tag','ForceSameLength',...
              'Value',0,...
marwan's avatar
marwan committed
              'ToolTip','Fills parameter vectors with NaN in order to get the same vector lengths.',...
marwan's avatar
marwan committed
              'String','Force equal data length.');

    uicontrol(props.checkbox, props.checkbox_CheckNewWindow, ...
              'Tag','CheckNewWindow',...
              'Value',0,...
marwan's avatar
marwan committed
              'ToolTip','Every apply click will create a new figure.',...
marwan's avatar
marwan committed
              'String','Create new figure.');

    % Knopf 5 :: Help
    uicontrol(props.button, props.button_help,...
              'Tag','help',...   
              'String','Help',...
              'Callback', 'mgui help',...
              'ToolTip','Helpwindow.', 'ButtonDownFcn','helpwin mgui');

    % Knopf 6 :: Close
    uicontrol(props.button, props.button_close, ...
              'Tag','close',...
              'String','Close',...
              'Callback', 'mgui close',...
              'ToolTip','Closing the GUI.', 'ButtonDownFcn','helpwin mgui');

    % Knopf 1 :: Ausfhren
    uicontrol(props.button, props.button_apply, 'Tag','apply',...  
              'String','Apply',...
              'Enable','off',...
              'Callback', 'mgui apply',...
              'ToolTip','Apply the choosen function.', 'ButtonDownFcn','helpwin mgui');

    h=axes;
    logo=load('logo');
    imagesc([logo.logo fliplr(logo.logo)], 'ButtonDownFcn','helpwin mgui')
    set(h,props.logo)

    h=uicontrol(props.text, props.text_disclaimer, ...
              'HandleVis','off',...
              'Tag','disclaimer',...
marwan's avatar
marwan committed
               'ButtonDownFcn','helpwin mgui');
marwan's avatar
marwan committed
    h2=textwrap(h,{[char(169),' AGNLD'],'University of Potsdam','2002-2006'});
marwan's avatar
marwan committed
    set(h,'String',h2)
    
 
     mgui mod2;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  case 'mod2'

    %%% erlegt den ganzen init-kram, wenn eine Funktion ausgewhlt wurde

    % Anzahl der Parameter und Auswahlboxen beim Start anpassen und zeichnen
    % lesen der Anzahl der minimalen/maximalen Parameter
    FN_Min  = funktion(select).min;
    FN_Max  = funktion(select).max;
    FN_Akt  = funktion(select).akt;
    FN_Name = funktion(select).name;

%%% kurzhilfe einblenden
    if strcmp(FN_Name, 'add programme');
      fnhelptext='ADD PROGRAMME   Adds a programme into the MGUI and its related RC file.';
      ind=length(fnhelptext);
    else
      fnhelptext=help(char(FN_Name));
      ind=findstr(char(10),fnhelptext);
    end

    hfh=findobj('Tag','funktion_helptext');
    if isempty(hfh)
      hfh=uicontrol(props.text, props.text_funktion_helptext,...
                'Tag','funktion_helptext',...
marwan's avatar
marwan committed
                'String',fnhelptext(1:ind(1)),...
                'ButtonDownFcn','helpwin mgui');
marwan's avatar
marwan committed
    else
marwan's avatar
marwan committed
      set(hfh,'string',fnhelptext(1:ind(1)))
marwan's avatar
marwan committed
    end
    h=get(hfh,'Extent');h2=get(hfh,'Position');
    set(hfh,'Position',[h2(1:2) h(3) h2(4)])
    drawnow

    %% Knppe in Ausgangsstellung bringen
    % Wenn, mehrere Parameter erlaubt sind, die Knpfe aktivieren
    % Es sind mehr Parameter mglich

    set(findobj('Tag','parm_le'),'Visible','on');
    set(findobj('Tag','parm_ge'),'Visible','on');

    if (FN_Akt == FN_Max)
      set(findobj('Tag','parm_ge'),'Visible','off');
    end

    if (FN_Akt == FN_Min)
      set(findobj('Tag','parm_le'),'Visible','off');
    end

    check_workspace;
    check_apply

    %% aufdecken 
    for i = 1 : FN_Akt
      hparm = findobj('Tag',['button_parm' num2str(i)]);
      hlbox = findobj('Tag',['listbox_parm' num2str(i)]);
      hbcp  = findobj('Tag',['button_close_parm' num2str(i)]);
      set(hparm,'Visible','on');
      set(hlbox,'Visible','on');
      set(hbcp,'Visible','on');
    end

    %% Verdeckt
    for i = (FN_Akt +1) : 4 % HACK: es sind nicht mehr als 4 paramter vorgesehen
      hparm = findobj('Tag',['button_parm' num2str(i)]);
      hlbox = findobj('Tag',['listbox_parm' num2str(i)]);
      hbcp  = findobj('Tag',['button_close_parm' num2str(i)]);
      set(hparm,'Visible','off');
      set(hlbox,'Visible','off');
      set(hbcp,'Visible','off');
    end

    if strcmp(FN_Name, 'load');
       mgui load
    end

    if strcmp(FN_Name, 'add programme');
       mgui add_programme
    end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%       
  case 'apply' 
    %%% Alle Parameter werden ausgelesen und hintereinander zusammengestellt und ausgefhrt
             
    %% Init
    FN_Akt = funktion(select).akt;
    FktName = char(funktion(select).name);

    %% 'save' und 'load' rausfischen -> selbst geschrieben
    if strcmp(FktName,'save')
      mgui save;
      return;
    end 

    if strcmp(FktName,'load')
      mgui load;
      return;
    end 

    if strcmp(FktName, 'add programme');
      mgui add_programme;
      return;
    end

    %% Auszufhrenden String zusammenbasteln
    Argumente = '(';
    for j=1 : FN_Akt
      Arg = ['Arg',num2str(j)];
%    %% Init
      var_list = get(findobj('Tag',['listbox_parm' num2str(j)]),'UserData');
      temp=funktion(select).klicklist;
marwan's avatar
marwan committed
      if ~isempty(temp), klick_list=temp{j}; else klick_list=[]; end
marwan's avatar
marwan committed
      if isempty(klick_list), klick_list=get(findobj('Tag',['button_close_parm', num2str(j)]),'UserData'); end

      Matrix = []; % Zu speichernder Inhalt

      %% Parameterinhalt zusammenstellen
      for i = 1:size(klick_list,2); 
        % Einlesen des Variablennamens und zuweisen seines Inhaltes
        VarName = var_list(klick_list(1,i)).name;

        try
          VarValue = evalin('base',VarName);
        catch
          h=warndlg(['Variable ' VarName ' could not be found and will be ignored henceforth.'],'Scan workspace'); 
marwan's avatar
marwan committed
          set(h,props.msgboxwin),h2=guihandles(h);set(h2.OKButton,props.msgbox)
marwan's avatar
marwan committed
          set(h,'Tag','helpdlgbox','HandleVisibility','On');
          continue;
        end

        % Zeilenvektor in Spaltenvektor transformieren 
        if (size(VarValue,1)) == 1
          VarValue = VarValue';
        end

        % Feststellen der gewnschten Reihen
marwan's avatar
marwan committed
        SelectRows = klick_list(klick_list(:,i) ~= 0,i);
marwan's avatar
marwan committed
        SelectRows(1,:) = []; % Lsche Kopf (Enthlt Verweis auf den Variablennamen)

        % Wenn Spalten gewhlt wurden, dann den Varibleninhalt darauf begrenzen
        if ~isempty(SelectRows)
           VarValue = VarValue(:,SelectRows);
        end

        % Unterschiedliche Lnge -> Auffllen mit "NaN"
        if (size(Matrix,1) ~= size(VarValue,1)) & (size(Matrix,1) ~= 0)
          % Matrix zu lang
          if (size(Matrix,1) > size(VarValue,1))  
             VarValue((size(VarValue,1)+1):size(Matrix,1),:) = NaN;
          % Spaltenvektor zu lang
          else
            Matrix((size(Matrix,1)+1):size(VarValue,1),:) = NaN;
          end
        end

        Matrix = [Matrix VarValue]; 
      end


    % zusammenfgen aller gewhlten Variablen

        % Wertzuweisung eines Paramters
        eval([Arg '= Matrix;']);

        Argumente = strcat(Argumente, Arg);

%        if j < FN_Akt
        Argumente = strcat(Argumente,',');    
%        end    
    end
    Argumente(end) = [];
    Argumente = strcat(Argumente,')');

    %% Alle Parameter erhalten die gleiche Lnge (wenn gewnscht)
    if get(findobj('Tag','ForceSameLength'),'Value')

      % Init
      MaxLength = 0;

      % Suche des lngsten Parameters
      for i = 1:FN_Akt
        Matrix = eval(['Arg' num2str(i)]);
        if (size(Matrix, 1) > MaxLength)
          MaxLength = size(Matrix, 1);
        end
      end

      % Alle Parameter auf die gleiche Lnge trimmen
      for i = 1:FN_Akt
        Matrix = eval(['Arg' num2str(i)]); % laden
        Matrix(size(Matrix,1):MaxLength,:)= NaN;
        eval([['Arg' num2str(i)] '= Matrix;']); % speichern
      end

    end

    %% Ausgabe der Funktionswerte
    % Erstellt auf Wunsch ein neues Ausgabefenster
    hnpw = findobj('Tag','CheckNewWindow');
    bNewWindow = get(hnpw,'Value');
    if bNewWindow
        figure(props.newwindow,'Tag','PrintWindow');
    end

    %% Ausfhren der ganzen Mhe (mit Errorhandling)

    % Schreibschutz aktivieren 
    set(hMF, 'HandleVisibility','Off');
    % Ausfhren 

    try
      set(0,props.root);
      eval([FktName Argumente]);
%       if ~isempty(ans)
%         assignin('base','ans',ans)
%       end
    catch
      set(0,'defaultUIControlBackgroundColor',props.msgbox.BackgroundColor,...
      'defaultUIControlForegroundColor',props.msgbox.ForegroundColor,...
      'defaultTextColor',props.text.ForegroundColor)
      h=errordlg(lasterr,['Call ',upper(FktName)]);
      set(h,props.msgboxwin),h2=guihandles(h);set(h2.OKButton,props.msgbox)
      waitfor(h);
    end

    % Schreibschutz deaktivieren
    set(hMF, 'HandleVisibility','on');
    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  case 'ChangeNumberOfParameters'

    %%% verndern der aktuellen Parameteranzahl
    %%% parm_le => verringern; parm_ge => vergroessern

    %% Init
    hcall  = gcbo;
    caller = get(hcall,'Tag');

    FN_Akt = funktion(select).akt;
    FN_Min = funktion(select).min;
    FN_Max = funktion(select).max;
    
    %% Anzahl sichtbarer Parameter verringern
marwan's avatar
marwan committed
    if strcmp('parm_le',caller)
marwan's avatar
marwan committed
      if FN_Akt > FN_Min 
        set(findobj('Tag',['button_parm' num2str(FN_Akt)]),'Visible','off');
        set(findobj('Tag',['button_close_parm' num2str(FN_Akt)]),'Visible','off');
        set(findobj('Tag',['listbox_parm' num2str(FN_Akt)]),'Visible','off');

        funktion(select).akt = FN_Akt - 1;
        set(hfs,'UserData',funktion);
        if funktion(select).akt <= FN_Min
          set(hcall,'Visible','off');
        end
        set(findobj('Tag','parm_ge'),'Visible','on');
      end

      %if ~isempty(get(findobj('Tag',['button_close_parm', num2str(FN_Akt-1)]),'UserData'))
      %  set(findobj('Tag','apply'),'Enable','on');
      %end
    %% Anzahl sichtbarer Parameter erhhen (parm_ge)
    else
      if FN_Akt < FN_Max 
        funktion(select).akt = FN_Akt + 1;

        set(findobj('Tag',['button_parm' num2str(FN_Akt+1)]),'Visible','on');
        set(findobj('Tag',['button_close_parm' num2str(FN_Akt+1)]),'Visible','on');
        set(findobj('Tag',['listbox_parm' num2str(FN_Akt+1)]),'Visible','on');

        set(hfs,'UserData',funktion);
        if funktion(select).akt >= FN_Max 
          set(hcall,'Visible','off');
        end
        set(findobj('Tag','parm_le'),'Visible','on');
        % Den Apply-Knopf deaktivieren
       % if isempty(get(findobj('Tag',['button_close_parm', num2str(FN_Akt+1)]),'UserData'))
       %   set(findobj('Tag','apply'),'Enable','off');
       % end
      end
    end

    %% Alle "Klick_listen" durchsuchen, ob "Apply" ausgefhrt werden darf
    FN_Akt   = funktion(select).akt;

    trigger = 0;
    % Alle "Klick_list"en durchsuchen
    for i = 1 : FN_Akt
      hbcp = findobj('Tag',['button_close_parm' num2str(i)]);
      temp=funktion(select).klicklist;
marwan's avatar
marwan committed
      if ~isempty(temp), klick_list=temp{i}; else klick_list=[]; end
marwan's avatar
marwan committed
      if isempty(klick_list), klick_list=get(hbcp,'UserData'); end
      if isempty(klick_list)
        trigger = 1;
      end
    end
    % Wenn mindestens eine leere dabei, dann die Ausfhrung verhindern
    if trigger
      set(findobj('Tag','apply'),'Enable','off');
    else
      set(findobj('Tag','apply'),'Enable','on');
    end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%        
  case 'Vis'
    %%% Macht die geforderte Listbox + Closebutton sichtbar

    % Aufrufender = akt. Parameter wird rot gefrbt
    hcall = gcbo;

    %% Bestimmen des zugehrigen Auswahlfensters und Closebuttons
    caller = get(hcall,'Tag');
    caller_lbox = strrep(caller,'button','listbox');
    caller_bc   = strrep(caller,'button','button_close');

    lokal_ws=check_workspace;

%%%%%%%%%%%%%%
% check for erased variables
marwan's avatar
marwan committed
nparm = str2double(strrep(caller,'button_parm',''));                             
marwan's avatar
marwan committed
tmplist = funktion(select).klicklist{nparm};                                  
tmpstr = funktion(select).klickstring{nparm};                                 
if ~isempty(tmpstr)                                                           
ParmBereiche =[0 find(tmpstr == ',') size(tmpstr,2)+1];                       
                                                                             
% jeden Parameter einzeln checken                                             
for i = 1:size(ParmBereiche,2)-1                                              
  varname = tmpstr(ParmBereiche(i)+1:ParmBereiche(i+1)-1);                    
  trigger = 0;                                                                
  for j = 1:size(lokal_ws,2)                                                  
    if strcmp(lokal_ws(j).name,varname)                                       
      trigger =1;                                                             
      tmplist(1,i) = j;                                                       
      break;                                                                  
    end                                                                       
  end                                                                         
  if trigger == 0                                                             
    tmplist(i) = NaN;                                                         
    tmpstr(ParmBereiche(i)+1:ParmBereiche(i+1)-1) = ' ';                      
  end                                                                         
end                                                                           
marwan's avatar
marwan committed
tmplist(isnan(tmplist(1,:))) = [];                                      
marwan's avatar
marwan committed
end                                                                           
%% hier noch die richtigen pfade eintragen                                    
%set(findobj('tag',caller_bc),'UserData',tmplist);                            
%set(hparm,'String',tmpstr);                                                  
funktion(select).klicklist{nparm}= tmplist;                                   
funktion(select).klickstring{nparm}=tmpstr;                                   
set(hfs,'UserData',funktion);                                                 
%%%%%%%%%%%%%%


    %% Aktivieren der angeforderten Listbox + Closebutton
    % Erst Listbox
marwan's avatar
marwan committed
    hlbox=findobj('Tag',caller_lbox);
marwan's avatar
marwan committed
    set(hlbox,'Visible','on');
%    set(hlbox,'String',[Ausgabe]);
%    set(hlbox,'UserData',lokal_ws);
    if isempty(get(hcall,'tooltip'))
      set(hlbox,'Value',[]); % beim oeffnen ist nichts ausgewaehlt
    end

    % Dann Closebutton
marwan's avatar
marwan committed
    hbcp=findobj('Tag',caller_bc);
marwan's avatar
marwan committed
    set(hbcp,'Visible','on');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  case 'UnVis'
    %%% macht die Listbox + Closebutton unsichtbar

    %% bestimmen des zugehrigen Auswahlfensters und Closebuttons
    hcall = gcbo;
    caller = get(hcall,'Tag');
    caller_lbox = strrep(caller,'button_close','listbox');

    %% deaktivieren der angeforderten Elemente
marwan's avatar
marwan committed
    hlbox=findobj('Tag',caller_lbox);
marwan's avatar
marwan committed
    set(hlbox,'Visible','off');
    set(hcall,'Visible','off');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  case 'help'
    %%% Es werden die die Standart-Matlab-Hilfetexte angezeigt

    %% Entsprechenden Hilfetext auswhlen und anzeigen
    if ~isempty(select)
      % 'Load' und 'Save' haben eigenen Hilfstext
      if strcmp(funktion(select).name,'load') | strcmp(funktion(select).name,'save') | strcmp(funktion(select).name,'add programme')
        if strcmp(funktion(select).name,'load')
          h=helpdlg(['Load workspace variables from an extern data file.',char(10),'Additionally text lines (e.g. header lines) will be skipped.'],'Help');
        elseif strcmp(funktion(select).name,'save') % (funktion(select).name == 'SAVE')
          h=helpdlg(['Save workspace variables into an extern data file.',char(10),'The file format will be plain ASCII.'],'Help');
        else
marwan's avatar
marwan committed
          h=helpdlg(['Adds a new programme into the function list of this MGUI.',char(10),...
                     'The minimal and maximal number of input parameters for this',char(10),...
                     'programme has to be specified. Finally, this changing will', char(10),...
                     'be done in the mgui.rc file.'],'Help');
        end
        set(h,props.msgboxwin),h2=guihandles(h);set(h2.OKButton,props.msgbox)
         set(h,'Tag','helpdlgbox','HandleVisibility','On');
marwan's avatar
marwan committed
   else
      % Hilfstext
        eval(['helpwin ', char(funktion(select).name)]);
      end
    else % wenn keine Funktion ausgewhlt wurde
      helpwin mgui;
    end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  case 'close'
    % beendet das Programm
    close(hMF)
    warning on
    % clear all
    tx{1}='Thank you for your blind trust.';
marwan's avatar
marwan committed
    tx{2}='Do you really like this?';
marwan's avatar
marwan committed
    tx{3}='Thanks, that you believe in us.';
    tx{4}='We hope we could satisfy you.';
    tx{5}='Thank you for working with us.';
    tx{6}='Now you should go home.';
    tx{7}='Did you already find us in the WorldWideWeb?';
marwan's avatar
marwan committed
    tx{8}='Your cat might be hungry.';
    tx{9}='Maybe your Matlab license will be needed by others?';
    tx{10}='Can you imagine, how long we have worked for that?';
    tx{11}='Thank you for your responsiveness.';
    tx{12}='Now ready for publishing?';
    disp(tx{round((length(tx)-1)*rand)+1})
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  case 'winclose'
    % koennte noetig werden, wenn noch irgendwelche settings 
    % zurueckgestellt werden muessen
    set(0,props.root);
    warning on

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  case 'klickreihenfolge'

    %%% Die Klickreihenfolge wird im parm_button_close gespeichert

    %% Bestimmung des Aufrufers
    hlbox = gcbo;

    % die passenden Handel initialisieren
    tmp = strrep(get(hlbox,'Tag'),'listbox','button_close');
marwan's avatar
marwan committed
    hbcp   = findobj('Tag',tmp);
marwan's avatar
marwan committed

    tmp = strrep(get(hlbox,'Tag'),'listbox','button');
marwan's avatar
marwan committed
    hparm = findobj('Tag',tmp);
marwan's avatar
marwan committed

    %% Init
marwan's avatar
marwan committed
    nparm = str2double(strrep(get(gcbo,'Tag'),'listbox_parm',''));
marwan's avatar
marwan committed
    temp=funktion(select).klicklist;
    if ~isempty(temp), 
       klick_list=temp{nparm}; 
    else
       klick_list = [];
    end
    if isempty(klick_list), klick_list = get(hbcp,'UserData'); end
    klick_akt  = get(hlbox,'Value');
marwan's avatar
marwan committed
    if isempty(klick_akt), return; end; % Keine Variable gewhlt
marwan's avatar
marwan committed

    var_list   = get(hlbox,'UserData');
marwan's avatar
marwan committed
    if isempty(var_list(1).name), return; end; % Keine Variable vorhanden
marwan's avatar
marwan committed

    %% Akualisieren der Klick_list (und anzeigen)
    % Wenn ausversehen mehr als ein Wert gewhlt wurde
    if (size(klick_akt,2) > 1)
      h=errordlg('Choose only one variable.','Parameter selection');
      set(h,props.msgboxwin),h2=guihandles(h);set(h2.OKButton,props.msgbox)
      waitfor(h)
      set(hlbox,'Value',klick_list(1,:));
      return;
    else

    % Aktualisieren
    if isempty(klick_list)
      posi=[];
    else
      posi = find(klick_list(1,:) == klick_akt);
    end

    if(isempty(posi))
      % Einfgen
      klick_list(1,(end +1)) = klick_akt;
    else
      % Lschen
      klick_list(:,posi) = [];
    end

    % Das der Nutzer sein klicken sieht (Listbox)
    set(hlbox,'Value',klick_list(1,:));

marwan's avatar
marwan committed
    klick_string=make_klick_string(klick_list,var_list);
marwan's avatar
marwan committed

       set(hparm,'String',klick_string);
       set(hparm,'ToolTip',klick_string);

      % Speichern der ganzen Mhe
      set(hbcp,'UserData',klick_list);
      funktion(select).klicklist{nparm}=klick_list;
      funktion(select).klickstring{nparm}=klick_string;
      set(hfs,'UserData',funktion)
    end
     
    check_apply

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  case('selectcolumn')
    
    %%% Hier erfolgt die Auswahl der Spalten

    %% Bestimmung des Aufrufers
    hcall = gcbo;
    caller= get(hcall,'Tag');

    % die passenden Handel initialisieren
    tmp   = strrep(caller,'listbox','button_close');
marwan's avatar
marwan committed
    hbcp   = findobj('Tag',tmp);
marwan's avatar
marwan committed

    hlbox = hcall;

    tmp = strrep(caller,'listbox','button');
marwan's avatar
marwan committed
    hparm = findobj('Tag',tmp);
marwan's avatar
marwan committed

    %% Init
marwan's avatar
marwan committed
    nparm = str2double(strrep(get(hlbox,'Tag'),'listbox_parm',''));
marwan's avatar
marwan committed
    temp=funktion(select).klicklist;
marwan's avatar
marwan committed
    if ~isempty(temp), klick_list=temp{nparm}; else klick_list = []; end
marwan's avatar
marwan committed
    if isempty(klick_list), klick_list = get(hbcp,'UserData'); end
marwan's avatar
marwan committed
    if isempty(klick_list), return; end; % Keine Variable ausgewhlt
marwan's avatar
marwan committed
    var_list   = get(hlbox,'UserData');
marwan's avatar
marwan committed
    if isempty(var_list(1).name), return; end; % Keine Variable vorhanden
marwan's avatar
marwan committed

    klick_string = [];
    VarName = var_list(klick_list(1,end)).name;
    VarValue = evalin('base',VarName);
    [Zeilen Spalten] = size(VarValue); % Begrenzungen ermitteln