How can I set a global variable in public properties App Designer that depends on another one?
49 ビュー (過去 30 日間)
古いコメントを表示
Dear all,
I have a problem on understanding how properly set a global variable in App Designer.
Indeed, I am using App Designer that at the button pressing will run a waving sinusoid in the UIaxes. Since this sinusoid will be called in another function I want that its parameters are usable and visible also in other functions.
I saw in other guides that to do this the variable can be set in the Public properties. However if I write:
properties (Access = public)
Fs = 60; % samples per second
dt = 1/Fs; % seconds per sample
StopTime = 60; % seconds
t = (0:dt:StopTime-dt)'; % seconds
Fc = 0.47;
x = 300*sin(2*pi*Fc*(t))+((app.BWEditField.Value+20)*9.81); % Starting wave
end
As you can see, I would keep global and visible all the parameters that allows to build a sinusoid. However, when I run, it gives an error on dt because it says that it doesn't find Fs. It seems like that it does not run in line the code...
Simplyfing. The question is: How can I declare globally in the properties the variables X and Y, with Y that is defined using X?
0 件のコメント
採用された回答
Steven Lord
2020 年 12 月 9 日
When defining properties' default values, you can only use the values of other properties if those properties are Constant. To access them you need to use the name of the class, which for purposes of this answer I'll call myappclass.
properties (Access = public, Constant)
Fs = 60; % samples per second
dt = 1/myappclass.Fs; % seconds per sample
StopTime = 60; % seconds
t = (0:myappclass.dt:myappclass.StopTime-myappclass.dt)'; % seconds
Fc = 0.47;
Those properties can be given values in the definition as they don't depend on the state of a particular instance of the class. The last property you tried to define cannot be given a default value in the properties block as it depends on the state of a particular instance of the class. You can define this property but you can't give it a value here. You could set its value in the constructor or in a regular method.
x = 300*sin(2*pi*Fc*(t))+((app.BWEditField.Value+20)*9.81); % Starting wave
end
0 件のコメント
その他の回答 (1 件)
Eric Sargent
2018 年 4 月 23 日
編集済み: Eric Sargent
2020 年 12 月 9 日
You need to define them as public properties and set the static values. You need to declare ALL of the variables you want/need to use in subsequent functions within the app.
[update]
Note, in the comments below, "app.tend" is used in StartupFcn but "tend" is not defined in properties)
properties (Access = public)
Fs = 60; % samples per second
StopTime = 60; % seconds
Fc = 0.47;
dt;
t;
x;
end
Then initialize the dependent variables in a startupFcn callback and refer to each of the variables as "app.<variablename>", such as:
function startupFcn(app)
app.dt = 1/app.Fs; % seconds per sample
app.t = (0:app.dt:app.StopTime-app.dt)'; % seconds
app.x = 300*sin(2*pi*app.Fc*(app.t))+((app.BWEditField.Value+20)*9.81); % Starting wave
end
Use the same dot notation in your subsequent code to access those variables.
plot(app.UIAxes,app.t,app.x)
2 件のコメント
参考
カテゴリ
Help Center および File Exchange で Develop Apps Using App Designer についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!