Passing objects and self to methods
29 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I'm creating objects within objects and methods within all of these. I'm having some trouble figuring out why my code is referencing the wrong input and trying to find properties for the wrong object. The main object is a satelliteModel, within in I have created a powerSystem and a commandSystem. commandSystem has two properties (socSafe and socUnsafe) and powerSystem has one property (soc). commandSystem has a method called command, which will take in an indexing variable t, powerSystem, and commandSystem and output a command depending on a loop, not included here because its just a lot of if statements (see code 1 below). I reference this code in a method within the satelliteModel class (see code 2). However, doing this gives me the following error (see error below). I have tried removing and renaming variables however I occasionally get the error of 'too many or too few input arguements' so an explanation there would also be greatly appreciated.
%%% CODE 1
function [command] = command(t, powerSystem, commandSystem, obj)
%%% command
% Takes in some data and returns a command
% INPUTS:
% t % Current Time Index
% powerSystem % Power System (object)
% obj % Command System (object)
% OUTPUTS:
% command % Command (integer)
% 0: Safety Mode
% 1: Nothing Mode
% 2: Charging Mode
% 3: Communication Mode
% 4: Experiment Mode
%%% CURRENT BOOLEANS
disp(powerSystem)
disp(commandSystem.socSafe)
disp(commandSystem.socUnsafe)
end
%%% CODE 2
function [] = simulate(obj)
%%% simulate
% Simulation for satellite model
%%% FIRST STEP
obj.stateS(1,:) = obj.stateI;
%%% SIMULATION LOOP
for a = 1:length(obj.time)-1
% [dState] = satelliteSystemDynamics(dt, state, obj, t)
t = obj.time(a);
% command(t, powerSystem, commandSystem)
command = obj.commandSystem.command(t, obj.powerSystem, obj.commandSystem); %%% PROBLEMS HERE
obj.stateS(a+1,:) = RK4(@obj.satelliteSystemDynamics, obj.dt, obj.stateS(a,:), t, command);
end
end
%%% ERROR
Unrecognized method, property, or field 'socSafe' for class 'powerSystem'.
Error in commandSystem/command (line 44)
disp(commandSystem.socSafe)
Error in satelliteModel/simulate (line 68)
command = obj.commandSystem.command(t, obj.powerSystem, obj.commandSystem);
0 Kommentare
Antworten (1)
Hornett
am 12 Sep. 2023
I understand that you are facing error while calling t
In MATLAB, when you call a method from an object, you need to accept the object itself (or a reference to the current object) as the first parameter in the method. The first parameter will always refer to the object instance, and after that, you can have your additional parameters. In other languages like C++, this is passed implicitly.
In your case, when you call this function from command = obj.commandSystem.command(t, obj.powerSystem, obj.commandSystem);, The parameters received will be as follows:
t = obj
powerSystem = t
commandSystem = obj.powerSystem
obj = obj.commandSystem
So, when this line runs ‘ disp(commandSystem.socSafe)’, it is referring to the ‘powerSystem’ object, hence the error message "Unrecognized method, property, or field 'socSafe' for class 'powerSystem'.
To fix the issue, you can define your function like the following example
function [command] = command(obj, t, powerSystem, commandSystem)
By rearranging the parameters and accepting the object as the first parameter, you can access the properties and methods of the correct objects within the method.
This change ensures that the method receives the correct object references and resolves the error you encountered.
To know more about passing object in a method, I suggest referring to the following documentation:
I hope this help you resolve the issue.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!