How to declare a global variable in Startup function of matlab app.designer

I want to declare a global variable in matlab app designer's startup function in order to use it continuously through the app's code.
Specifically, I want to declare value1 as a global variable
I have tried that but it doesn't seems that works.
Thank you very much

 Accepted Answer

Do not use global variables, especially in AppDesigner.
Instead, declare a public or private property accessible anywhere within the app or outside of the app (for public properties).

7 Comments

Thanks for your reply, however it doesn't seems that works. I don't know what am I doing wrong.
I just want to declare a global variable (or better, after reading your answer, a public value) to use it along the program. I want value1 to be that variable and set its value to 7, so when I click the button, the value appears on the editfield.
Here is set as a public value
Here is the callback of the button to work when it is pussed.
When I run the app and click the button..nothing happnes, just pops out that error.
Thanks for your help.
If the "global variable" is needed within the app, it should be declared as a private variable, accessible by app.____.
If the "global variable" is needed by another app or function outside of the app, then it should be a public variable, accessible using the same method above after retrieving the app handle.
It doesn't let me put that. Appears me in red as an error
Thanks for your reply also Adam Danz.
But I have tried exactly the same both codes that I have putted here in private property and the same happens
You're not declaring the property correctly. Check out the "step-by-step guide" link in my answer. It shows you pictures of each step.
Thanks for your reply!
What I was doing wrong was that I was putting in the button callback:
app.editnum.Value=value1; %INCORRECT
Instead of
app.editnum.Value=app.value1; %CORRECT
Thanks!

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2019b

Community Treasure Hunt

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

Start Hunting!