GUI apps design for newbie
4 views (last 30 days)
Show older comments
Hello everyone,
I am new in using matlab, and unfamiliar with the programming language. Currently, I made a GUI using apps design that user need to insert 2 value and when click the pushbutton of CALCULATE; the value of TOTAL and MINIMUM and MAXIMUM value will automated calculate and appear. But sadly, I don't know what coding to insert in callback. Can someone help me, it seems easy but i really need someone's help because i am newbie in this field, sorry for any inconveniece. Here I paste the coding that I already generated from my developed GUI;
Thank you so much.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1624058/image.png)
classdef Testing < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
CalculateButton matlab.ui.control.Button
CO2EditField matlab.ui.control.NumericEditField
CO2EditFieldLabel matlab.ui.control.Label
EditField_4 matlab.ui.control.NumericEditField
EditField_3 matlab.ui.control.NumericEditField
EditField_2 matlab.ui.control.NumericEditField
EditField matlab.ui.control.NumericEditField
MaximumLabel matlab.ui.control.Label
MinimumLabel matlab.ui.control.Label
TOTALLabel matlab.ui.control.Label
FabricationLabel matlab.ui.control.Label
MaterialLabel matlab.ui.control.Label
ParametersLabel matlab.ui.control.Label
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 640 480];
app.UIFigure.Name = 'MATLAB App';
% Create ParametersLabel
app.ParametersLabel = uilabel(app.UIFigure);
app.ParametersLabel.HorizontalAlignment = 'center';
app.ParametersLabel.FontWeight = 'bold';
app.ParametersLabel.Position = [56 417 70 22];
app.ParametersLabel.Text = 'Parameters';
% Create MaterialLabel
app.MaterialLabel = uilabel(app.UIFigure);
app.MaterialLabel.HorizontalAlignment = 'center';
app.MaterialLabel.FontWeight = 'bold';
app.MaterialLabel.Position = [170 417 50 22];
app.MaterialLabel.Text = 'Material';
% Create FabricationLabel
app.FabricationLabel = uilabel(app.UIFigure);
app.FabricationLabel.HorizontalAlignment = 'center';
app.FabricationLabel.FontWeight = 'bold';
app.FabricationLabel.Position = [260 417 70 22];
app.FabricationLabel.Text = 'Fabrication';
% Create TOTALLabel
app.TOTALLabel = uilabel(app.UIFigure);
app.TOTALLabel.HorizontalAlignment = 'center';
app.TOTALLabel.FontWeight = 'bold';
app.TOTALLabel.Position = [364 417 44 22];
app.TOTALLabel.Text = 'TOTAL';
% Create MinimumLabel
app.MinimumLabel = uilabel(app.UIFigure);
app.MinimumLabel.HorizontalAlignment = 'center';
app.MinimumLabel.FontWeight = 'bold';
app.MinimumLabel.Position = [442 417 58 22];
app.MinimumLabel.Text = 'Minimum';
% Create MaximumLabel
app.MaximumLabel = uilabel(app.UIFigure);
app.MaximumLabel.HorizontalAlignment = 'center';
app.MaximumLabel.FontWeight = 'bold';
app.MaximumLabel.Position = [524 417 60 22];
app.MaximumLabel.Text = 'Maximum';
% Create EditField
app.EditField = uieditfield(app.UIFigure, 'numeric');
app.EditField.Position = [250 389 80 22];
% Create EditField_2
app.EditField_2 = uieditfield(app.UIFigure, 'numeric');
app.EditField_2.Position = [346 389 80 22];
% Create EditField_3
app.EditField_3 = uieditfield(app.UIFigure, 'numeric');
app.EditField_3.Position = [431 389 80 22];
% Create EditField_4
app.EditField_4 = uieditfield(app.UIFigure, 'numeric');
app.EditField_4.Position = [514 389 80 22];
% Create CO2EditFieldLabel
app.CO2EditFieldLabel = uilabel(app.UIFigure);
app.CO2EditFieldLabel.HorizontalAlignment = 'right';
app.CO2EditFieldLabel.Position = [82 389 30 22];
app.CO2EditFieldLabel.Text = 'CO2';
% Create CO2EditField
app.CO2EditField = uieditfield(app.UIFigure, 'numeric');
app.CO2EditField.Position = [156 389 80 22];
% Create CalculateButton
app.CalculateButton = uibutton(app.UIFigure, 'push');
app.CalculateButton.Position = [515 341 82 23];
app.CalculateButton.Text = 'Calculate';
% 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 = Testing
% Create UIFigure and 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
3 Comments
Accepted Answer
bo zhang
on 22 Feb 2024
Edited: bo zhang
on 22 Feb 2024
I notice that the EditField you used is numeric type, so the value is numeric and there is no need to str2double.
function CalculateButtonPushed(app, event)
disp('CalculateButtonPushed callback executed');
% Get the values from the edit boxes
value1 = app.CO2EditField.Value; % dont use str2double, bcs the EditField is numeric already
value2 = app.EditField.Value;
disp(['Value1: ' num2str(value1)]);
disp(['Value2: ' num2str(value2)]);
% Check if the input is valid (numeric)
if isnan(value1) || isnan(value2)
% Handle the case when the input is not numeric
% You can display an error message or take other actions
disp('Invalid input. Please enter numeric values.');
return;
end
% Perform calculations
totalValue = value1 + value2;
minValue = min(value1, value2);
maxValue = max(value1, value2);
% Display the results in the text boxes
app.EditField_2.Value = totalValue;
app.EditField_3.Value = minValue;
app.EditField_4.Value = maxValue;
disp(['Total: ' num2str(totalValue)]);
disp(['Minimum: ' num2str(minValue)]);
disp(['Maximum: ' num2str(maxValue)]);
end
4 Comments
bo zhang
on 16 May 2024
Sorry for just notice your new question.
I am not familiar with the FIS, but after reading your code, I noticed several issues: Firstly, you need to specify the membership functions for 'High', 'Low', and 'Medium', because the Rules you give metion that. Secondly, if the output variable is 'trimf', it should only have three parameters for its membership function, but you provided five parameters [0 0.25 0.5 0.75 1]. Thirdly, The ways you called the FIS functions are not in accordance with the function definition rules in MATLAB https://ww2.mathworks.cn/help/releases/R2023a/fuzzy/working-from-the-command-line.html?searchHighlight=MembershipFunctions&s_tid=doc_srchtitle. I suggest referring to the documentation for guidance. Lastly, but most importantly, the FIS should not be placed within a button's callback function. It should be defined as a property of the program within the methods section.
bo zhang
on 16 May 2024
Edited: bo zhang
on 16 May 2024
I have made modifications to the FIS part of the code. I defined the membership functions 'Low', 'Medium', and 'High' using gaussmf, referring the range you provided, but the standard deviation and mean values I used are not rigorous (just gave some ramdom value to test it works or not). After the modifications, the FIS should be able to run. However, the specific performance of the FIS still needs adjustments and modifications based on your actual requirements.
Here is the code (well works in MATLAB R2024a):
inputRanges = [0.0002002 0.0009065; 0.0001934 0.001008]; % Input ranges for SO2 and NOx
outputRange = [0 1]; % Output range for APMaterialindex
% Define input variable names
inputNames = {'SO2', 'NOx'};
% Define output variable name
outputName = 'APMaterialindex';
% Define membership functions for input variables
inputMFs = {
'trimf', [0.0002002 0.0005534 0.0009065]; % Membership function for SO2: Triangular
'trimf', [0.0001934 0.0006007 0.001008] % Membership function for NOx: Triangular
};
% Define membership functions for output variable
outputMFs = {
'trimf', [0 0.25 0.5 0.75 1] % Membership function for APMaterialindex: Triangular
};
% Define fuzzy rules
ruleList = [
"IF SO2 IS High AND NOx IS High THEN APMaterialindex IS Low";
"IF SO2 IS High AND NOx IS Medium THEN APMaterialindex IS Low";
"IF SO2 IS High AND NOx IS Low THEN APMaterialindex IS Medium";
"IF SO2 IS Medium AND NOx IS High THEN APMaterialindex IS Medium";
"IF SO2 IS Medium AND NOx IS Medium THEN APMaterialindex IS Medium";
"IF SO2 IS Medium AND NOx IS Low THEN APMaterialindex IS Medium";
"IF SO2 IS Low AND NOx IS High THEN APMaterialindex IS Medium";
"IF SO2 IS Low AND NOx IS Medium THEN APMaterialindex IS Medium";
"IF SO2 IS Low AND NOx IS Low THEN APMaterialindex IS High";
];
% Create FIS object
fis = mamfis;
% Add input(1) membership functions
fis = addInput(fis,inputRanges(1,:),'Name',inputNames{1}, 'MFType', inputMFs{1, 1});
x = linspace(inputRanges(1,1),inputRanges(1,2),100);
guessstd = 0.3*std(x); % give the standard deviation for gaussmf
fis = addMF(fis,inputNames{1},"gaussmf",[guessstd mean(inputMFs{1, 2}(1:2))],"Name","Low");
fis = addMF(fis,inputNames{1},"gaussmf",[guessstd inputMFs{1, 2}(2)],"Name","Medium");
fis = addMF(fis,inputNames{1},"gaussmf",[guessstd inputMFs{1, 2}(3)],"Name","High");
% Add input(2) membership functions
fis = addInput(fis,inputRanges(2,:),'Name',inputNames{2}, 'MFType', inputMFs{2, 1});
x = linspace(inputRanges(2,1),inputRanges(2,2),100);
guessstd = 0.3*std(x); % give the standard deviation for gaussmf
fis = addMF(fis,inputNames{2},"gaussmf",[guessstd mean(inputMFs{2, 2}(1:2))],"Name","Low");
fis = addMF(fis,inputNames{2},"gaussmf",[guessstd inputMFs{2, 2}(2)],"Name","Medium");
fis = addMF(fis,inputNames{2},"gaussmf",[guessstd inputMFs{2, 2}(3)],"Name","High");
% Add output(1) membership functions
fis = addOutput(fis,outputRange,'Name',outputName, 'MFType', outputMFs{1, 1});
x = linspace(outputRange(1,1),outputRange(1,2),100);
guessstd = 0.3*std(x); % give the standard deviation for gaussmf
fis = addMF(fis,outputName,"gaussmf",[guessstd 0.25],"Name","Low");
fis = addMF(fis,outputName,"gaussmf",[guessstd 0.75],"Name","Medium");
fis = addMF(fis,outputName,"gaussmf",[guessstd 1.0 ],"Name","High");
% Add fuzzy rules
fis = addRule(fis, ruleList);
% Get input data from edit fields
SO2 = 0.00055335;
NOx = 0.0006007;
input_data = [SO2, NOx];
% Perform fuzzy inference
AP_Performance = evalfis(fis,input_data); % Pass input data and FIS
More Answers (0)
See Also
Categories
Find more on Develop uifigure-Based Apps in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!