Write a Function in GUI
조회 수: 9 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (1개)
Praveen Reddy
2023년 3월 14일
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:
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Develop uifigure-Based Apps에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!