How can I set a global variable in public properties App Designer that depends on another one?
    18 views (last 30 days)
  
       Show older comments
    
    Alessandro Frongia
 on 27 Mar 2018
  
    
    
    
    
    Answered: Steven Lord
    
      
 on 9 Dec 2020
            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 Comments
Accepted Answer
  Steven Lord
    
      
 on 9 Dec 2020
        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 Comments
More Answers (1)
  Eric Sargent
    
 on 23 Apr 2018
        
      Edited: Eric Sargent
    
 on 9 Dec 2020
  
      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 Comments
See Also
Categories
				Find more on Develop Apps Using App Designer in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
