Clear Filters
Clear Filters

Write a Function in GUI

9 views (last 30 days)
Majdouline Bouzbib
Majdouline Bouzbib on 4 Nov 2018
Answered: Praveen Reddy on 14 Mar 2023
Hello ,
I would like to represent a variation of parameters with Sliders and show the result of a polynom function.
Im using the version R2018b. I started by putting sliders (parameters) and a Button , so that if you press the button the Preis will be calculated and shown in a text field. Now i want to write my function but i dont know where to place it exactly in the code(Below).
Thanks in advance
classdef app1 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
EEGSliderLabel matlab.ui.control.Label
EEGSlider matlab.ui.control.Slider
KWKGSliderLabel matlab.ui.control.Label
KWKGSlider matlab.ui.control.Slider
NetzentgeltSliderLabel matlab.ui.control.Label
NetzentgeltSlider matlab.ui.control.Slider
VerbrauchSliderLabel matlab.ui.control.Label
VerbrauchSlider matlab.ui.control.Slider
AnteilEESliderLabel matlab.ui.control.Label
AnteilEESlider matlab.ui.control.Slider
BeschaffungskostenSliderLabel matlab.ui.control.Label
BeschaffungskostenSlider matlab.ui.control.Slider
StrompreisEditFieldLabel matlab.ui.control.Label
StrompreisEditField matlab.ui.control.NumericEditField
CalculateButton matlab.ui.control.StateButton
end
methods (Access = private)
function results = func(app)
end
end
methods (Access = private)
% Value changed function: CalculateButton
function CalculateButtonValueChanged(app, event)
value = app.CalculateButton.Value;
end
end
% App initialization and construction
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure
app.UIFigure = uifigure;
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'UI Figure';
% Create EEGSliderLabel
app.EEGSliderLabel = uilabel(app.UIFigure);
app.EEGSliderLabel.HorizontalAlignment = 'right';
app.EEGSliderLabel.Position = [417 280 29 22];
app.EEGSliderLabel.Text = 'EEG';
% Create EEGSlider
app.EEGSlider = uislider(app.UIFigure);
app.EEGSlider.Limits = [0 10];
app.EEGSlider.Orientation = 'vertical';
app.EEGSlider.Position = [467 289 3 150];
% Create KWKGSliderLabel
app.KWKGSliderLabel = uilabel(app.UIFigure);
app.KWKGSliderLabel.HorizontalAlignment = 'right';
app.KWKGSliderLabel.Position = [296 281 42 22];
app.KWKGSliderLabel.Text = 'KWKG';
% Create KWKGSlider
app.KWKGSlider = uislider(app.UIFigure);
app.KWKGSlider.Limits = [0 1];
app.KWKGSlider.Orientation = 'vertical';
app.KWKGSlider.Position = [359 290 3 150];
% Create NetzentgeltSliderLabel
app.NetzentgeltSliderLabel = uilabel(app.UIFigure);
app.NetzentgeltSliderLabel.HorizontalAlignment = 'right';
app.NetzentgeltSliderLabel.Position = [146 280 67 22];
app.NetzentgeltSliderLabel.Text = 'Netzentgelt';
% Create NetzentgeltSlider
app.NetzentgeltSlider = uislider(app.UIFigure);
app.NetzentgeltSlider.Limits = [0 10];
app.NetzentgeltSlider.Orientation = 'vertical';
app.NetzentgeltSlider.Position = [234 289 3 150];
% Create VerbrauchSliderLabel
app.VerbrauchSliderLabel = uilabel(app.UIFigure);
app.VerbrauchSliderLabel.HorizontalAlignment = 'right';
app.VerbrauchSliderLabel.Position = [22 282 60 22];
app.VerbrauchSliderLabel.Text = 'Verbrauch';
% Create VerbrauchSlider
app.VerbrauchSlider = uislider(app.UIFigure);
app.VerbrauchSlider.Limits = [0 700];
app.VerbrauchSlider.Orientation = 'vertical';
app.VerbrauchSlider.Position = [103 291 3 150];
% Create AnteilEESliderLabel
app.AnteilEESliderLabel = uilabel(app.UIFigure);
app.AnteilEESliderLabel.HorizontalAlignment = 'right';
app.AnteilEESliderLabel.Position = [212 70 53 22];
app.AnteilEESliderLabel.Text = 'Anteil EE';
% Create AnteilEESlider
app.AnteilEESlider = uislider(app.UIFigure);
app.AnteilEESlider.Orientation = 'vertical';
app.AnteilEESlider.Position = [286 79 3 150];
% Create BeschaffungskostenSliderLabel
app.BeschaffungskostenSliderLabel = uilabel(app.UIFigure);
app.BeschaffungskostenSliderLabel.HorizontalAlignment = 'right';
app.BeschaffungskostenSliderLabel.Position = [1 77 115 22];
app.BeschaffungskostenSliderLabel.Text = 'Beschaffungskosten';
% Create BeschaffungskostenSlider
app.BeschaffungskostenSlider = uislider(app.UIFigure);
app.BeschaffungskostenSlider.Limits = [0 10];
app.BeschaffungskostenSlider.Orientation = 'vertical';
app.BeschaffungskostenSlider.Position = [137 86 3 150];
% Create StrompreisEditFieldLabel
app.StrompreisEditFieldLabel = uilabel(app.UIFigure);
app.StrompreisEditFieldLabel.HorizontalAlignment = 'right';
app.StrompreisEditFieldLabel.Position = [361 175 64 22];
app.StrompreisEditFieldLabel.Text = 'Strompreis';
% Create StrompreisEditField
app.StrompreisEditField = uieditfield(app.UIFigure, 'numeric');
app.StrompreisEditField.Position = [440 175 100 22];
% Create CalculateButton
app.CalculateButton = uibutton(app.UIFigure, 'state');
app.CalculateButton.ValueChangedFcn = createCallbackFcn(app, @CalculateButtonValueChanged, true);
app.CalculateButton.Text = 'Calculate';
app.CalculateButton.Position = [361 214 100 22];
end
end
methods (Access = public)
% Construct app
function app = app1
% Create and configure components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
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

Answers (1)

Praveen Reddy
Praveen Reddy on 14 Mar 2023
Hi Majdouline,
I understand that you want to associate a logic for the Calculate button in the UI. You can add a call back function (Button pushed function) for the Calculate button. In the call back function, perform the required calculations and set the value of text field.
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: CalculateButton
function CalculateButtonPushed(app, event)
end
end
To know more about how to add a call back function, please refer to the following MATLAB documentation:

Categories

Find more on Develop uifigure-Based Apps in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!