How can I set a global variable in public properties App Designer that depends on another one?
조회 수: 54 (최근 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!