How can I set a global variable in public properties App Designer that depends on another one?
43 次查看(过去 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 uifigure-Based Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!