How to fix this error ? "attempt to reference field of non-structure array"
Show older comments
Hello, I had this error by declaring testset and classify as global variables. do you have an idea ? thank you
<<

>>
1 Comment
Aaah, global variables and buggy code (that is almost impossible to debug).
Where is the variable defined? How many functions can access it? When and how is its value changed?
So many questions, and so few answers. Globals are the least recommended way of passing data between workspaces, and come second on this list of bad code practices:
Buggy, and all experts avoid using them... why would anyone even want to use them?
Answers (2)
Stephen's comment is exhaustive. Let me summarize it:
- Obviously the variable testset has not been defined before or not in the required way.
- Because it is a global variable, debugging is hard. You cannot directly find the code, which has (over-) written its value the last time.
- This is the expected and known problem with global variables. If the variables are provided as inputs, it is trivial to use the debugger to find the source of the last change.
Poor solution: Dig in your code until you find the bad declaration of the global variable.
Good solution: Remove the global variables from your code and use a clean providing of inputs and outputs - or even more secure an object oriented approach.
4 Comments
houda mejri
on 29 Mar 2017
houda mejri
on 29 Mar 2017
Jan
on 29 Mar 2017
@houda: Now you can provide the dataset and teh classifier as inputs to myfunctiontominimize, see e.g. http://www.mathworks.com/matlabcentral/answers/1971
houda mejri
on 29 Mar 2017
Joshah Magdalene
on 9 Feb 2020
0 votes
I too have the same error.
Attempt to reference field of non-structure array.
I find this error in the below line of my code.
index = int32(pose.subset(j))+1;
I will be grateful if it is sorted out. Thank You!
4 Comments
Walter Roberson
on 9 Feb 2020
Whatever your pose variable is, it is not a struct. One thing to check is whether it is [] as there are sequences of instructions that can leave a variable [] when you do not expect
For example
pose = []
for i = 1 : length(x)
pose.data(i) = x(i).^2;
end
That sure looks like it sets pose as a struct with a field named data, but take a look at what happens if x is empty: length(emptiness) is 0 so the loop would not do any iterations and pose would be left as []
Walter Roberson
on 9 Feb 2020
You posted about this somewhere else and I answered there
Joshah Magdalene
on 9 Feb 2020
Thank you!
I have another query.
Is there any function like subset() and candidate() in Matlab?
If so what do they do?
Thanks in advance!
Walter Roberson
on 9 Feb 2020
I do not find functions by those names in MATLAB. Possibly some third-party toolbox.
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!