![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/794459/image.png)
MatLab App Design - how to use default value for Edit Field function?
48 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
meryem berrada
el 8 de Nov. de 2021
Comentada: meryem berrada
el 9 de Nov. de 2021
Hello,
I have an Edit Field box on the Design view and I opened a callback in the Code View. There is a default value in the Edit Field box that I would like to be used automatically when a user runs the app.
I have this:
% Value changed function: LengthEditField
function LengthEditFieldValueChanged(app, event)
app.L = app.LengthEditField.Value;
if isempty(app.L)
new_value = 3; % set default value to 3
end
app.L=new_value;
end
But when I use this variable in an equation, it tells me that its "empty (0x0) double".
How do I get it to display a value of 3 for example ?
Thank you,
0 comentarios
Respuesta aceptada
Cris LaPierre
el 8 de Nov. de 2021
Editada: Cris LaPierre
el 8 de Nov. de 2021
The code you have shared is for capturing the value in the edit field, not setting it. Also, keep in mind that callback functions only execute when they are called (typically by the user interacting with the component).
Therefore, the best way to set a default value for a component is through its properties in the Component Browser.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/794459/image.png)
It is possible to set a value programmatically, but remember that the code will only be run when the callback function it has been added to is executed.
app.LengthEditField.Value = 3
11 comentarios
Cris LaPierre
el 9 de Nov. de 2021
Editada: Cris LaPierre
el 9 de Nov. de 2021
Here is what is happening. You define app.Lp and app.I as so:
% Value changed function: WdiscLengthmEditField
function WdiscLengthmEditFieldValueChanged(app, event)
app.Lp= app.WdiscLengthmEditField.Value;
end
% Value changed function: CurrentAEditField
function CurrentAEditFieldValueChanged(app, event)
app.I = app.CurrentAEditField.Value;
end
That means app.Lp and app.I will not be assigned a value unless their corresponding callback function is executed. If the variable, which is defined in Properties, is never assigned a value, it will be empty.
Perhaps the solution, then, is to get rid of all the callbacks that are just for assigning values, and instead capture the corresponding component values inside the function where you need it. See attached.
function RunButtonPushed(app, event)
% Get values
app.L = app.LengthEditField.Value;
app.errL = app.lengtherror.Value;
app.D = app.DiameterEditField.Value;
app.errD = app.diametererror.Value;
app.Lp= app.WdiscLengthmEditField.Value;
app.Dp = app.WdiscDiametermEditField.Value;
app.n3 = app.OutliersDegreesofFreedomEditField.Value;
app.I = app.CurrentAEditField.Value;
app.Lnum = app.LorenzNumberWK2EditField.Value;
Más respuestas (0)
Ver también
Categorías
Más información sobre Develop Apps Using App Designer en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!