classdef trussGUI1 < matlab.apps.AppBase

  % Properties that correspond to app components
  properties (Access = public)
    UIFigure   matlab.ui.Figure
    Label5     matlab.ui.control.Label
    Label4     matlab.ui.control.Label
    Label3     matlab.ui.control.Label
    EditField  matlab.ui.control.NumericEditField
    Label2     matlab.ui.control.Label
    Label1     matlab.ui.control.Label
  end

  
  properties (Access = private)
    Truss % Description
    Freq % Description
  end
  

  % Callbacks that handle component events
  methods (Access = private)

    % Code that executes after component creation
    function startupFcn(app)
      app.Truss = loadTruss("bruecke");
      [M, C] = createMatrices(app.Truss);
      [Phi, app.Freq] = computeEigenvalues(M, C);
      
      % Setze Frequenz für Startwert 1
      fe = app.Freq(1);
      sFreq = sprintf("%8.6f", fe);
      app.Label4.Text = sFreq;    
    end

    % Value changed function: EditField
    function EditFieldValueChanged(app, event)
      modeNr = app.EditField.Value;
      if (1 <= modeNr) && (modeNr <= 2*app.Truss.N)
        fe = app.Freq(modeNr);
        sFreq = sprintf("%8.6f", fe); % Eigenfrequenz als String
        app.Label4.Text = sFreq;
      end
    end
  end

  % Component initialization
  methods (Access = private)

    % Create UIFigure and components
    function createComponents(app)

      % Create UIFigure and hide until all components are created
      app.UIFigure = uifigure('Visible', 'off');
      app.UIFigure.Position = [100 100 405 205];
      app.UIFigure.Name = 'MATLAB App';

      % Create Label1
      app.Label1 = uilabel(app.UIFigure);
      app.Label1.BackgroundColor = [1 1 1];
      app.Label1.HorizontalAlignment = 'center';
      app.Label1.FontSize = 16;
      app.Label1.Position = [69 169 276 22];
      app.Label1.Text = 'Eigenschwingungen eines Fachwerks';

      % Create Label2
      app.Label2 = uilabel(app.UIFigure);
      app.Label2.BackgroundColor = [1 1 1];
      app.Label2.Position = [119 133 170 22];
      app.Label2.Text = 'Ordnung der Eigenschwingung';

      % Create EditField
      app.EditField = uieditfield(app.UIFigure, 'numeric');
      app.EditField.ValueChangedFcn = createCallbackFcn(app, @EditFieldValueChanged, true);
      app.EditField.HorizontalAlignment = 'center';
      app.EditField.BackgroundColor = [1 0.902 0.902];
      app.EditField.Position = [156 84 100 22];
      app.EditField.Value = 1;

      % Create Label3
      app.Label3 = uilabel(app.UIFigure);
      app.Label3.BackgroundColor = [1 1 1];
      app.Label3.Position = [119 39 86 22];
      app.Label3.Text = 'Eigenfrequenz:';

      % Create Label4
      app.Label4 = uilabel(app.UIFigure);
      app.Label4.BackgroundColor = [1 1 1];
      app.Label4.Position = [204 39 60 22];
      app.Label4.Text = '-1.234567';

      % Create Label5
      app.Label5 = uilabel(app.UIFigure);
      app.Label5.BackgroundColor = [1 1 1];
      app.Label5.Position = [263 39 25 22];
      app.Label5.Text = 'Hz';

      % Show the figure after all components are created
      app.UIFigure.Visible = 'on';
    end
  end

  % App creation and deletion
  methods (Access = public)

    % Construct app
    function app = trussGUI1

      % Create UIFigure and components
      createComponents(app)

      % Register the app with App Designer
      registerApp(app, app.UIFigure)

      % Execute the startup function
      runStartupFcn(app, @startupFcn)

      if nargout == 0
        clear app
      end
    end

    % Code that executes before app deletion
    function delete(app)

      % Delete UIFigure when app is deleted
      delete(app.UIFigure)
    end
  end
end