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 ... ?
Cordelia David
2019-4-5
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?
Cordelia David
2019-4-5
Cordelia David
2019-4-5
A. Sawas
2019-4-6
Where the "Mission" is comming from?
In the function definition the variable name is Gravity
Stephen23
2019-4-6
"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
采纳的回答
更多回答(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!