App designer: How to define self-referring properties?

6 次查看(过去 30 天)
I'm getting into app designer once again, and right from the start I'm getting desperate.
When defining a property, I receive a nasty error message, I don't really comprehend.
Unrecognized function or variable 'x'
Referring to the little thing I have written:
properties (Access = private)
x = linspace(0,1);
VStep = ones(1,length(x));
VWell= zeros(1,length(x));
end
Why this result into an error? Actually it should simply define these variables to be accessible for future functions, but for some reason it won't pass x as a variable. I tried app.x, x.value etc and ALOT of other variations, but why Matlab makes it that complicated?
Where is the non-intuitive logic that will make it work?
  2 个评论
Rik
Rik 2023-1-27
How did you make sure this is even possible without inheriting from a parent class?
Niklas Kurz
Niklas Kurz 2023-1-27
What other classes? Why not only using and passing the variables as they are defined in the current property?
With much more tinkering I figured the following opaque code will work:
classdef QuantumPotential < matlab.apps.AppBase
properties (Access = private,Constant)
x = linspace(0,1);
VStep = ones(1,length(QuantumPotential.x));
VWell= zeros(1,length(QuantumPotential.x));
end
end
Where
classdef YourAppName < matlab.apps.AppBase
...
end
should be set by default.

请先登录,再进行评论。

采纳的回答

Steven Lord
Steven Lord 2023-1-27
That code does not define a variable named x. It defines an object property named x.
You're assuming that defining one property on an object can use the values of other properties on that object, but what guarantee do you have about the order in which those properties are defined? What if MATLAB tried to create the value for the property VStep before it created the value for the property x?
You can do what you want using Constant properties. These are properties that, as the attribute name implies, are constant across all instances of that class. This means you can refer to them using the class name rather than needing to query a class instance. In the example below x and Y are defined using the Hidden private Constant property N.
q = example1901875
q =
example1901875 with properties: x: [0 0.0101 0.0202 0.0303 0.0404 0.0505 0.0606 0.0707 0.0808 0.0909 0.1010 0.1111 0.1212 0.1313 0.1414 0.1515 0.1616 0.1717 0.1818 0.1919 0.2020 0.2121 0.2222 0.2323 0.2424 0.2525 0.2626 0.2727 0.2828 0.2929 0.3030 0.3131 0.3232 0.3333 0.3434 … ] Y: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  1 个评论
Niklas Kurz
Niklas Kurz 2023-1-27
But in normal script/ code it will work like that. A continuous execution of coding-lines.
If y uses x before x is defined - the script will fail. So why not transfering the same logic to properties?
How did you actually defined the property?
properties (Access = Hidden,private,Constant) ?
I'm not familiar to that property. So far only the complicated syntax that I stated above is accepted. I am open for simpler solutions.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Construct and Work with Object Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by