How to Plot Membership Function inside App Designer Axes?

3 visualizaciones (últimos 30 días)
Ahmad Azzam Hafidz
Ahmad Azzam Hafidz el 7 de Oct. de 2021
Editada: TED MOSBY el 13 de Jun. de 2025
So I made a simple fuzzy blower speed controller with room temperature input using App Designer, what I want is to plot the membership function (plotmf) 'input' and 'output' into app.UIAxes and app.UIAxes2, but what happens instead is matlab plotting to figure 1, the code is as follows
classdef app3 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
UIAxes matlab.ui.control.UIAxes
CalculateLabel matlab.ui.control.Label
Enter matlab.ui.control.Switch
TemperatureLabel matlab.ui.control.Label
TemperatureEditField matlab.ui.control.NumericEditField
SpeedLabel matlab.ui.control.Label
SpeedEditField matlab.ui.control.NumericEditField
UIAxes2 matlab.ui.control.UIAxes
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
end
% Value changed function: Enter
function EnterValueChanged(app, event)
a=newfis('controller')
a=addvar(a,'input','temp',[0 30])
a=addmf(a,'input',1,'cold','trimf',[0 0 10]);
a=addmf(a,'input',1,'cool','trimf',[0 12.5 17.5]);
a=addmf(a,'input',1,'pleasent','trimf',[15 17.5 20]);
a=addmf(a,'input',1,'warm','trimf',[17.5 22.5 27.5]);
a=addmf(a,'input',1,'hot','trimf',[25 30 30]);
a=addvar(a,'output','speed',[0 100]);
a=addmf(a,'output',1,'minimal','trimf',[0 0 30]);
a=addmf(a,'output',1,'slow','trimf',[10 30 50]);
a=addmf(a,'output',1,'medium','trimf',[40 50 60]);
a=addmf(a,'output',1,'fast','trimf',[50 70 90]);
a=addmf(a,'output',1,'blast','trimf',[70 100 100]);
ruleList=[ ...
1 1 1 1
2 2 1 1
3 3 1 1
4 4 1 1
5 5 1 1];
a=addrule(a,ruleList);
x = app.TemperatureEditField.Value;
y= evalfis([x], a);
% the problem
axes(app.UIAxes)
plotmf(a,'input',1)
hold on
plot(app.UIAxes,x,0.2,'o');
plot(app.UIAxes,x,0.5,'o');
plot(app.UIAxes,x,0.8,'o');
hold off
%the problem
axes(app.UIAxes2);
plotmf(a,'output',1)
hold on
plot(app.UIAxes2,y,0.2,'o');
plot(app.UIAxes2,y,0.5,'o');
plot(app.UIAxes2,y,0.8,'o');
hold off
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 640 480];
app.UIFigure.Name = 'MATLAB App';
% Create UIAxes
app.UIAxes = uiaxes(app.UIFigure);
title(app.UIAxes, 'Temperature')
xlabel(app.UIAxes, 'Temp')
ylabel(app.UIAxes, 'Degree of Membership')
app.UIAxes.Position = [1 296 308 185];
% Create CalculateLabel
app.CalculateLabel = uilabel(app.UIFigure);
app.CalculateLabel.HorizontalAlignment = 'center';
app.CalculateLabel.Position = [175.5 91 56 22];
app.CalculateLabel.Text = 'Calculate';
% Create Enter
app.Enter = uiswitch(app.UIFigure, 'slider');
app.Enter.ValueChangedFcn = createCallbackFcn(app, @EnterValueChanged, true);
app.Enter.Position = [180 128 45 20];
% Create TemperatureLabel
app.TemperatureLabel = uilabel(app.UIFigure);
app.TemperatureLabel.HorizontalAlignment = 'right';
app.TemperatureLabel.Position = [59 230 73 22];
app.TemperatureLabel.Text = 'Temperature';
% Create TemperatureEditField
app.TemperatureEditField = uieditfield(app.UIFigure, 'numeric');
app.TemperatureEditField.Position = [147 230 100 22];
% Create SpeedLabel
app.SpeedLabel = uilabel(app.UIFigure);
app.SpeedLabel.HorizontalAlignment = 'right';
app.SpeedLabel.Position = [92 183 40 22];
app.SpeedLabel.Text = 'Speed';
% Create SpeedEditField
app.SpeedEditField = uieditfield(app.UIFigure, 'numeric');
app.SpeedEditField.Position = [147 183 100 22];
% Create UIAxes2
app.UIAxes2 = uiaxes(app.UIFigure);
title(app.UIAxes2, 'Speed')
xlabel(app.UIAxes2, 'Speed')
ylabel(app.UIAxes2, 'Degree of Membership')
app.UIAxes2.Position = [333 296 308 185];
% 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 = app3
% 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

Respuestas (1)

TED MOSBY
TED MOSBY el 13 de Jun. de 2025
Editada: TED MOSBY el 13 de Jun. de 2025
Hi,
The function "plotmf" always draws into the current figure that belongs to MATLAB’s traditional graphics system.
axes(app.UIAxes)
plotmf(fis,'input',1) % still opens Figure 1
The workaround is to let "plotmf" give you the raw data and then plot that data yourself on the axes that belong to the app:
[xOut,mfOut] = plotmf(fis,variableType,variableIndex);
Here is how you can fix your problem and apply it at the required places in your code:
[xin, mfin] = plotmf(fis,'input',1); % get data, no plotting
plot(app.UIAxes,xin,mfin,'LineWidth',1.2)
hold(app.UIAxes,'on')
plot(app.UIAxes,[T T],[0 1],'--r','LineWidth',1)
hold(app.UIAxes,'off')
Here's more information on "plotmf" :
Hope this helps!

Categorías

Más información sobre Develop Apps Using App Designer en Help Center y File Exchange.

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by