I have a LaunchSimulation class that has some dependent properties that need to be calculated whenever the property is referenced. I am trying to access these properties inside a button callback of my GUI file. I create my simulation object inside the openingFcn and am able to reference the regular properties just fine, but when I try and reference any dependent property, it says that the field does not exist.
How can I access the dependent properties? I am not too sure why they do not exist.
Below is my LaunchSimulation class:
classdef LaunchSimulation < handle %%Constants
properties (Constant) g = 9.81 % m/s^2
end %%Properties
properties launcher end %%Dependent Properties
properties (Dependent) horizontalRange verticalRange timeOfFlight end %%Non-static Methods
methods %%constructor
function this = LaunchSimulation(launcher) this.launcher = launcher; end %%Launch data computation for user input values
function computeHorizontalAndVerticalRange(this) this.launcher.launchVelocity = input('Enter launcher velocity: '); this.launcher.launchAngle = input('Enter launcher angle: '); DataDisplayer.displayLauncherSettingsTable(this); fprintf('Horizontal Range: %f \n', this.horizontalRange) fprintf('Vertical Range: %f \n', this.verticalRange) end %%Getters and Setters
% horizontalRange
function value = get.horizontalRange(this) value = ((this.launcher.launchVelocity)^2 * sind(2 * this.launcher.launchAngle)) / LaunchSimulation.g; end % verticalRange
function value = get.verticalRange(this) value = ((this.launcher.launchVelocity^2) * sind(this.launcher.launchAngle)^2) / (2 * LaunchSimulation.g); end % timeOfFlight
function value = get.timeOfFlight(this) value = ((2 * this.launcher.launchVelocity) * sind(this.launcher.launchAngle)) / LaunchSimulation.g; end end end
and here is the relevant code for the GUI file
OpeningFcn:
% --- Executes just before RocketLauncherSimulationGUI is made visible.
function RocketLauncherSimulationGUI_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to RocketLauncherSimulationGUI (see VARARGIN)
% Choose default command line output for RocketLauncherSimulationGUI
handles.output = hObject; % Update handles structure
guidata(hObject, handles); % Instantiate a launcher object with zeroed property values
launcher = Launcher(0,0,0,0); % Instantiate a simulation object
simulation = LaunchSimulation(launcher);
Here is where I try and reference the dependent properties in the callback:
% --- Executes on button press in buttonSimulate.
function buttonSimulate_Callback(hObject, eventdata, handles) % Set the launcher data
simulation.launcher.springConstant = get(handles.editSpringConstant, 'String'); simulation.launcher.projectileMass = get(handles.editProjectileMass, 'String'); simulation.launcher.launchVelocity = get(handles.editLaunchVelocity, 'String'); simulation.launcher.angle = get(handles.editLaunchAngle, 'String'); % This is the part that does not work. For some reason these fields do not exist
% Construct data matrix for table using simulation object
tableData = {simulation.launcher.springDisplacement ... % simulation.horizontalRange ...
% simulation.verticalRange ...
% simulation.timeOfFlight};
% Set the data of the table
set(handles.tableSimulationData, 'Data', tableData);
Best Answer