MatLab App Design - how to use default value for Edit Field function?

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,

 Respuesta aceptada

Cris LaPierre
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.
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

Hi Cris,
Thank you for answering this question quickly!
I did set the default value in the Component Browser. Are you saying that if I set my variable to be 3 in the properties, then it will stay 3 unless the Edit Field value is changed by the user ?
If you set the Value to 3 in the Component Browser, then when the app launches, the edit field will be prepopulated with a 3. That will be its value until the user changes it.
But when I hover my cursor on the variable it shows me that it is empty. This is why I thought that I had to add something in the callback. This does not solve my issue.
What variable is empty? Where are you hovering your cursor over it?
A few lines later I have this equation:
app.V=app.p*10^-8.*app.L.*app.I*4/(pi.*app.D.^2);
When I hover my cursor on "app.L" it shows me "empty (0x0) double". I would like it to be 3.
Please consider sharing your app. There are too many unknowns to say with any certainty what is going on. You can attach it using the paperclip icon.
Thank you for suggesting this. I also attached a data set excel document.
The line of code you mention does not exist in the app you have shared. the closest I could find is this on line 568:
app.Va=app.pW*10^-8.*app.Lp.*app.I*4/(pi.*app.Dp.^2);
That is the one. I just simplified it in my response to your previous comment. So my concern is that all the variables that are defined by the Edit Field seem to be empty. I am sorry if this problem is bigger than it seemed initially.
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;
Oh I understand now. That makes sense. Perfect. Thank you so much for your help!
You're the best!!
Thank you!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Develop Apps Using App Designer en Centro de ayuda y File Exchange.

Preguntada:

el 8 de Nov. de 2021

Comentada:

el 9 de Nov. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by