Why do I get the error: "Attempt to reference field of non-structure array"?

I refer to a function using a dataset transferred from stata, and I get the following message:
"Attempt to reference field of non-structure array.
Error in livsindkomst (line 26)
data(find(st_dataset.udd~=uddtype),:)=[];
end"
st_dataset is the data transferred from stata, but it seems like there is a problem refering to this data. Specifically, my aim is to check whether the variable udd equals the variable uddtype, and delete it if it does not. I define the variable uddtype in my programme.

2 Comments

Use the debugger, put a breakpoint on that line and see if st_dataset is actually a struct. It seems either it is something else or it is empty, if that is possible from the data transfer, which is technically a double.
It seems like that is the problem. Is there an easy way to make this conversion, or should I specify fields and values for each variable in my dataset?

Sign in to comment.

 Accepted Answer

"Is there an easy way to make this conversion". If this conversion refers to converting an empty double into an empty struct with the correct fields:
if isempty(st_dataset) && ~isstruct(st_dataset)
st_dataset = struct('udd', {}, 'otherfield', {}); %add as many fields as necessary
end
If you have the list of field names as a string in a cell array, it may be easier to use convert an empty cell array into an empty structure:
fields = {'udd', 'otherfield'}; %add as many fields as necessary
if isempty(st_dataset) && ~isstruct(st_dataset)
st_dataset = cell2struct(cell(0, 0, numel(fields)), fields, 3);
end

3 Comments

I think the problem is when the data is loaded. It gives me the following warning:
Warning: Variable 'st_dataset' originally saved as a dataset cannot be instantiated as an object and will be read in as a
uint32.
Is there anything I can do to load it as a struct?
The best would be to load it as type dataset. Do you really not have access to a MATLAB installation equipped with the Statistics Toolbox?
@Jacob, that is a completely different problem (It would have helped a lot to show that warning message from the start!).
Matlab without the Statistics Toolbox does not know how to read datasets objects stored in mat files. Since matlab does not know what to do with the data it reads, it just replaces it with a number (whose significance is not explained).
You need the stat toolbox to read that data.

Sign in to comment.

More Answers (1)

Matt J
Matt J on 20 Jul 2016
Edited: Matt J on 20 Jul 2016
You must verify that st_dataset really is a variable of the type that you intend it to be. MATLAB is complaining that it is not a variable that accepts dot-indexing syntax like st_dataset.udd. Maybe it is just a scalar number, for example.

Categories

Find more on Programming in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!