Reference To Non-Existent Field

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 个评论

"Wherein Gravity and PhysicalConstants are other structures that are in the Matlab path"
What exactly does this mean? Functions and classes can be on the MATLAB path, but a structure is an data type that exists within MATLAB memory (and hence do not exist by themselves as any file, although they can be stored within files):
It is not possible to have numeric arrays or char arrays or cell arrays or any other arrays by on the MATLAB path. The files that they might be stored within could certainly be on the MATLAB path: you should revise what the MATLAB Search Path is:
Are these actually structures actually saved (i.e. serialized) in a .mat file, or are they actually classes (whose properties can be accessed using the same syntax), or something else ... ?
I have them saved in .mat file separately from this code, but in the same folder. It is able to access the structures when inputted in the Command window. Does that help?
Cordelia David: Please show us how you are calling FruitCalculations. Clearly whatever variable you are providing as an input argument is not a structure with the expected field. So we need to know how you are calling this function and/or how you are defining that variable.
How are you importing the data from those .mat files?
I have PhysicalConstants as a separately saved .mat file that looks like this:
function PhysicalConstants = SetPhysicalConstant()
PhysicalConstants.Lightspeed = 299792458;
PhysicalConstants.Planck = 6.626068E-34;
PhysicalConstants.Boltzman = 1.3806504E-23;
end
And Gravity as a separately saved .mat file that looks like this:
function Gravity = SetGravityConstants()
Gravity.Detector = 35
Gravity.Optics = 4.5
end
And I am calling them in the function as shown in the first Question. I am then using the command file and there I am entering:
>>Gravity = SetGravityConstants
>>PhysicalConstants = SetPhysicalConstant
>>FruitCalc = FruitCalculations(Gravity,PhysicalConstants)
The Command Window takes the Gravity and PhysicalConstants and places them in the wrokspace with structures of the properly assigned variables. All of the values are easily seen in the workspace and can be accessed normally. However, when I enter the FruitCalc = FruitCalculations, it arrives at the following error:
Not enough input arguments.
Error in SNRCalculations (line 4)
det = Mission.Detector;
Or this:
Reference to non-existent field 'Detector'.
Error in SNRCalculations (line 4)
det = Mission.Detector;
My apologies, The Gravity Function looks more like this:
function Gravity = SetGravityConstants()
Gravity.Detector.Range = 35
Gravity.Optics.Color = 4.5
end
As you can probably tell, there are more variables than this and I have changed the variable names from the original code. The structure is the same though.
Where the "Mission" is comming from?
In the function definition the variable name is Gravity
@Cordelia David: this might indicate a problem:
"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

请先登录,再进行评论。

 采纳的回答

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

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Workspace Variables and MAT Files 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by