App designer: How to define self-referring properties?

7 ビュー (過去 30 日間)
Niklas Kurz
Niklas Kurz 2023 年 1 月 27 日
コメント済み: Niklas Kurz 2023 年 1 月 27 日
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 Center および File ExchangeDevelop Apps Using App Designer についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by