MATLAB: Reference To Non-Existent Field

debug4mestructuressubstructures

Hi Matlab community! I just started on my first job and they have me learning Matlab for a project. However, I don't code very often or very well, so I'm having some problems with a couple of things in my code. If anyone has any explanations for how to improve my code that would be great!
So far I have created five Matlab codes with multiple substructures. In two of them I call the other structures as variables in equations. It looks similar to this:
function FruitCalc = FruitCalculations( Gravity, PhysicalConstants )
% Assignment Statements For Ease
det = Gravity.Detector;
opt = Gravity.Optics;
phys = PhysicalConstants;
FruitCalc.Aerodynamics = (det.Strawberry - phys.Gravity)
FruitCalc.FruitsDropped = (FruitCalc.Aerodynamics/phys.Gravity)*det.Oranges;
end
Wherein Gravity and PhysicalConstants are other structures that are in the Matlab path. Here I assigned the substructures of Gravity.Detector to det, Gravity.Optics to opt, and PhysicalConstants = phys so that the longer calculations are more readable.
I keep getting the error in det=Gravity.Detector; of the following:
Reference to non-existent field 'Detector'.
Error in SNRCalculations (line 4)
det = Mission.Detector;
I believe it has something to do with assigning det and so on to substructures, but I'm not sure. If anyone has any ideas, or any ways I hsould improve my code, please help a girl out.

Best Answer

I have reproduced the error you see - sort of. Running FruitCalculations_main on R2018b throws an error
>> FruitCalculations_main
Reference to non-existent field 'Strawberry'.
Error in FruitCalculations (line 6)
FruitCalc.Aerodynamics = (det.Strawberry - phys.Gravity);
Error in FruitCalculations_main (line 4)
FruitCalc = FruitCalculations( Gravity, PhysicalConstants );
where the text below is saved in a file named, FruitCalculations_main.m, which is on the path
%%
Gravity = SetGravityConstants;
PhysicalConstants = SetPhysicalConstant;
FruitCalc = FruitCalculations( Gravity, PhysicalConstants );
and where the text below is saved in a file named, FruitCalculations.m, which is on the path
function FruitCalc = FruitCalculations( Gravity, PhysicalConstants )
% Assignment Statements For Ease
det = Gravity.Detector;
phys = PhysicalConstants;
FruitCalc.Aerodynamics = (det.Strawberry - phys.Gravity);
FruitCalc.FruitsDropped = (FruitCalc.Aerodynamics/phys.Gravity)*det.Oranges;
end
and where the text below is saved in a file named, SetGravityConstants.m, which is on the path
function Gravity = SetGravityConstants()
Gravity.Detector.Range = 35;
Gravity.Optics.Color = 4.5;
end
and where the text below is saved in a file named, SetPhysicalConstant.m, which is on the path (copy and paste is cheap)
function PhysicalConstants = SetPhysicalConstant()
PhysicalConstants.Lightspeed = 299792458;
PhysicalConstants.Planck = 6.626068E-34;
PhysicalConstants.Boltzman = 1.3806504E-23;
end
Comments
  1. The error should not surprise since the function, SetGravityConstants, doesn't create a field named Strawberry
  2. It's good that you created a Minimal Working Example. However, the error message you show doesn't come from this MWE. The names: SNRCalculations and Mission belongs to your full program - I guess