Reference To Non-Existent Field
    11 views (last 30 days)
  
       Show older comments
    
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.
7 Comments
  A. Sawas
      
 on 6 Apr 2019
				Where the "Mission" is comming from?
In the function definition the variable name is Gravity
  Stephen23
      
      
 on 6 Apr 2019
				"I have PhysicalConstants as a separately saved .mat file that looks like this:"
function PhysicalConstants = SetPhysicalConstant()
You seem to be confusing data files and program files:
- .mat files are binary files for storing data (i.e. numeric arrays, structure arrays, etc.),
- .m files are text files for storing functions, scripts, and classes, and are commonly called "M-files".
You keep describing that you have "saved .mat file", but what you have shown seem to be function definitions that should be saved in .m files. You should confirm that the file extensions are as I describe above. Please also show us the output of these commands:
which SetGravityConstants -all
which SetPhysicalConstant -all
Accepted Answer
  per isakson
      
      
 on 6 Apr 2019
        
      Edited: per isakson
      
      
 on 6 Apr 2019
  
      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
- The error should not surprise since the function, SetGravityConstants, doesn't create a field named Strawberry
- 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
0 Comments
More Answers (0)
See Also
Categories
				Find more on Workspace Variables and MAT Files 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!


