Update 3D plots when values are changed in app designer

4 views (last 30 days)
Hello, Im new with GUI's and Im working in a simple example where the user has to choose an object from the dropdown menu, then fill the atributes of the object and plot it in 3D.
So far, I managed to draw an sphere but I see an issue:
1.- Plot tends to update only when I change the dropdown menu. How can I enter a value and see the changes in the same moment?
Can someone help me? here is my code:
classdef EJ3 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
CalculoPanel matlab.ui.container.Panel
calcularvolumenButton matlab.ui.control.Button
VolumenEditField matlab.ui.control.NumericEditField
VolumenEditFieldLabel matlab.ui.control.Label
Label matlab.ui.control.Label
EntradadedatosPanel matlab.ui.container.Panel
MenuDropDown matlab.ui.control.DropDown
MenuDropDownLabel matlab.ui.control.Label
V4EditField matlab.ui.control.NumericEditField
V4EditFieldLabel matlab.ui.control.Label
V3EditField matlab.ui.control.NumericEditField
V3EditFieldLabel matlab.ui.control.Label
V2EditField matlab.ui.control.NumericEditField
V2EditFieldLabel matlab.ui.control.Label
V1EditField matlab.ui.control.NumericEditField
V1EditFieldLabel matlab.ui.control.Label
EspesorEditField matlab.ui.control.NumericEditField
EspesorEditFieldLabel matlab.ui.control.Label
BaseEditField matlab.ui.control.NumericEditField
BaseEditFieldLabel matlab.ui.control.Label
RadioEditField matlab.ui.control.NumericEditField
RadioEditFieldLabel matlab.ui.control.Label
AlturaEditField matlab.ui.control.NumericEditField
AlturaEditFieldLabel matlab.ui.control.Label
UIAxes matlab.ui.control.UIAxes
end
% Callbacks that handle component events
methods (Access = private)
% Value changed function: MenuDropDown
function MenuDropDownValueChanged(app, event)
menu = app.MenuDropDown.Value;
radius = app.RadioEditField.Value;
altura = app.AlturaEditField.Value;
switch menu
case 1
app.RadioEditField.Editable = 'On';
app.AlturaEditField.Editable ='Off';
app.V1EditField.Editable ='Off';
app.V2EditField.Editable ='Off';
app.V3EditField.Editable ='Off';
app.V4EditField.Editable ='Off';
app.BaseEditField.Editable ='Off';
app.EspesorEditField.Editable ='Off';
[X,Y,Z] = sphere;
X2 = X * radius;
Y2 = Y * radius;
Z2 = Z * radius;
surf(app.UIAxes, X2,Y2,Z2)
case 2
app.RadioEditField.Editable = 'On';
app.AlturaEditField.Editable ='On';
app.V1EditField.Editable ='Off';
app.V2EditField.Editable ='Off';
app.V3EditField.Editable ='Off';
app.V4EditField.Editable ='Off';
app.BaseEditField.Editable ='Off';
app.EspesorEditField.Editable ='Off';
[X,Y,Z] = cylinder(radius);
Z = Z*altura;
surf(app.UIAxes,X,Y,Z)
case 3
app.RadioEditField.Editable = 'Off';
app.AlturaEditField.Editable ='On';
app.V1EditField.Editable ='Off';
app.V2EditField.Editable ='Off';
app.V3EditField.Editable ='Off';
app.V4EditField.Editable ='Off';
app.BaseEditField.Editable ='On';
app.EspesorEditField.Editable ='On';
case 4
app.RadioEditField.Editable = 'Off';
app.AlturaEditField.Editable ='Off';
app.V1EditField.Editable ='On';
app.V2EditField.Editable ='On';
app.V3EditField.Editable ='On';
app.V4EditField.Editable ='On';
app.BaseEditField.Editable ='Off';
app.EspesorEditField.Editable ='Off';
end
end
% Button pushed function: calcularvolumenButton
function calcularvolumenButtonPushed(app, event)
radius= app.RadioEditField.Value;
VolumenEsfera= 4/3*pi*radius^3
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 970 724];
app.UIFigure.Name = 'MATLAB App';
% Create UIAxes
app.UIAxes = uiaxes(app.UIFigure);
title(app.UIAxes, 'Plot')
zlabel(app.UIAxes, 'Z')
app.UIAxes.Position = [462 131 474 411];
% Create EntradadedatosPanel
app.EntradadedatosPanel = uipanel(app.UIFigure);
app.EntradadedatosPanel.Title = 'Entrada de datos';
app.EntradadedatosPanel.Position = [22 165 449 356];
% Create AlturaEditFieldLabel
app.AlturaEditFieldLabel = uilabel(app.EntradadedatosPanel);
app.AlturaEditFieldLabel.HorizontalAlignment = 'right';
app.AlturaEditFieldLabel.Position = [221 212 37 22];
app.AlturaEditFieldLabel.Text = 'Altura';
% Create AlturaEditField
app.AlturaEditField = uieditfield(app.EntradadedatosPanel, 'numeric');
app.AlturaEditField.Limits = [0 Inf];
app.AlturaEditField.Position = [273 212 100 22];
% Create RadioEditFieldLabel
app.RadioEditFieldLabel = uilabel(app.EntradadedatosPanel);
app.RadioEditFieldLabel.HorizontalAlignment = 'right';
app.RadioEditFieldLabel.Position = [58 212 37 22];
app.RadioEditFieldLabel.Text = 'Radio';
% Create RadioEditField
app.RadioEditField = uieditfield(app.EntradadedatosPanel, 'numeric');
app.RadioEditField.Limits = [0 Inf];
app.RadioEditField.Position = [110 212 100 22];
% Create BaseEditFieldLabel
app.BaseEditFieldLabel = uilabel(app.EntradadedatosPanel);
app.BaseEditFieldLabel.HorizontalAlignment = 'right';
app.BaseEditFieldLabel.Position = [62 174 33 22];
app.BaseEditFieldLabel.Text = 'Base';
% Create BaseEditField
app.BaseEditField = uieditfield(app.EntradadedatosPanel, 'numeric');
app.BaseEditField.Position = [110 174 100 22];
% Create EspesorEditFieldLabel
app.EspesorEditFieldLabel = uilabel(app.EntradadedatosPanel);
app.EspesorEditFieldLabel.HorizontalAlignment = 'right';
app.EspesorEditFieldLabel.Position = [215 174 50 22];
app.EspesorEditFieldLabel.Text = 'Espesor';
% Create EspesorEditField
app.EspesorEditField = uieditfield(app.EntradadedatosPanel, 'numeric');
app.EspesorEditField.Position = [273 174 100 22];
% Create V1EditFieldLabel
app.V1EditFieldLabel = uilabel(app.EntradadedatosPanel);
app.V1EditFieldLabel.HorizontalAlignment = 'right';
app.V1EditFieldLabel.Position = [70 79 25 22];
app.V1EditFieldLabel.Text = 'V1';
% Create V1EditField
app.V1EditField = uieditfield(app.EntradadedatosPanel, 'numeric');
app.V1EditField.Position = [110 79 100 22];
% Create V2EditFieldLabel
app.V2EditFieldLabel = uilabel(app.EntradadedatosPanel);
app.V2EditFieldLabel.HorizontalAlignment = 'right';
app.V2EditFieldLabel.Position = [233 79 25 22];
app.V2EditFieldLabel.Text = 'V2';
% Create V2EditField
app.V2EditField = uieditfield(app.EntradadedatosPanel, 'numeric');
app.V2EditField.Position = [273 79 100 22];
% Create V3EditFieldLabel
app.V3EditFieldLabel = uilabel(app.EntradadedatosPanel);
app.V3EditFieldLabel.HorizontalAlignment = 'right';
app.V3EditFieldLabel.Position = [70 29 25 22];
app.V3EditFieldLabel.Text = 'V3';
% Create V3EditField
app.V3EditField = uieditfield(app.EntradadedatosPanel, 'numeric');
app.V3EditField.Position = [110 29 100 22];
% Create V4EditFieldLabel
app.V4EditFieldLabel = uilabel(app.EntradadedatosPanel);
app.V4EditFieldLabel.HorizontalAlignment = 'right';
app.V4EditFieldLabel.Position = [233 29 25 22];
app.V4EditFieldLabel.Text = 'V4';
% Create V4EditField
app.V4EditField = uieditfield(app.EntradadedatosPanel, 'numeric');
app.V4EditField.Position = [273 29 100 22];
% Create MenuDropDownLabel
app.MenuDropDownLabel = uilabel(app.EntradadedatosPanel);
app.MenuDropDownLabel.HorizontalAlignment = 'right';
app.MenuDropDownLabel.Position = [144 274 36 22];
app.MenuDropDownLabel.Text = 'Menu';
% Create MenuDropDown
app.MenuDropDown = uidropdown(app.EntradadedatosPanel);
app.MenuDropDown.Items = {'Esfera', 'Cilindro', 'Paralelepipedo', 'Piramide'};
app.MenuDropDown.ItemsData = [1 2 3 4];
app.MenuDropDown.ValueChangedFcn = createCallbackFcn(app, @MenuDropDownValueChanged, true);
app.MenuDropDown.Position = [195 274 141 22];
app.MenuDropDown.Value = 1;
% Create Label
app.Label = uilabel(app.UIFigure);
app.Label.WordWrap = 'on';
app.Label.Position = [121 520 729 111];
app.Label.Text = 'Esta aplicacion hace un plot de 4 figuras que se eligen mediante el menu dropdown. El usuario introduce los datos necesarios para cada figura y la app lo plotea de acuerdo a estos datos. Pulsando el boton calculo, se calcula el volumen de la figura ploteada';
% Create CalculoPanel
app.CalculoPanel = uipanel(app.UIFigure);
app.CalculoPanel.Title = 'Calculo';
app.CalculoPanel.Position = [22 26 914 106];
% Create VolumenEditFieldLabel
app.VolumenEditFieldLabel = uilabel(app.CalculoPanel);
app.VolumenEditFieldLabel.HorizontalAlignment = 'right';
app.VolumenEditFieldLabel.Position = [448 30 52 22];
app.VolumenEditFieldLabel.Text = 'Volumen';
% Create VolumenEditField
app.VolumenEditField = uieditfield(app.CalculoPanel, 'numeric');
app.VolumenEditField.Position = [515 30 100 22];
% Create calcularvolumenButton
app.calcularvolumenButton = uibutton(app.CalculoPanel, 'push');
app.calcularvolumenButton.ButtonPushedFcn = createCallbackFcn(app, @calcularvolumenButtonPushed, true);
app.calcularvolumenButton.Position = [273 30 106 22];
app.calcularvolumenButton.Text = 'calcular volumen';
% 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 = EJ3
% 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

Accepted Answer

Cris LaPierre
Cris LaPierre on 8 Jan 2022
The only callback function with plotting code in it is the dropdown menu callback function. Currently, this function only runs when your selection in the drop down menu changes.
However, callback functions are just functions. You can call them from other functions. I would create a callback for each component you want to trigger a new/updated plot, and call the dropdown menu callback from each.
% hypothetical component callback
function EditFieldValueChanged(app, event)
% call dropdown callback from within another callback
MenuDropDownValueChanged(app, event)
end

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!