Inserting fields into an empty structure
224 views (last 30 days)
Show older comments
I am writing a function to append new data to an existing structure (output). I want the results of every subject to be compared to the corresponding results of every other subject, but I need it to append the results onto an existing mat file and not overwrite it.
My attempt so far has been to create a new empty substructure (output.subnum), and then to add a file name and comparisons field. I want comparisons itself to be a structure, organized by ysubject comparison. The desired output structure I want is:
output
subjectX1
namesubjX1
w/subjY1
namesubjy1
results
z1
...zn
for every x and y.
Below is what I have so far attempted. n is the subject in the list I want to start with and y is the number of subjects I can do:
function output = hcp_compare(hcp,output,n,y)
for x=n:n+y
%create subject name
subnum=strcat('subj', (num2str(x)));
%then loop through evry other subject and compare to x
for y=1:length(hcp)
%create other subject name
comparison=strcat('withSubj',num2str(y));
%append new subject to existing subjects
output.(subnum)=struct([]);
output.(subnum).name=hcp(x,1).name;
output.(subnum).(comparison)=struct([]);
output.(subnum).(comparison).name=hcp(y,1).name;
disp([subnum comparison])
%loop through evry z cell and compare for evry xypair
for z=1:35778
output.(subnum).(comparison).results(z,1)=(hcp(x,1).connMatVect(z,1)-hcp(y,1).connMatVect(z,1));
end
end
end
end
However, when I do this, I get the following error:
??? A dot name structure assignment is illegal when the structure is empty.
Use a subscript on the structure.
Error in ==> hcp_compare_orig at 18
output.(subnum).name=hcp(x,1).name;
0 Comments
Answers (3)
Geoff Hayes
on 26 Sep 2016
This error occurs because you have explicitly created an empty structure and are then trying to assign a field to this structure. Unfortunately, the error message ...Use a subscript on the structure is not all that clear on what you should do next. So rather than creating an empty structure, why not create one with the fields that you need? Try replacing the two lines
output.(subnum)=struct([]);
output.(subnum).(comparison)=struct([]);
with
output.(subnum)=struct('name', '', comparison, struct('name',''));
With the above, you create a valid structure with the required fields but default the name fields to empty strings.
Try the above and see what happens!
2 Comments
Stephen23
on 9 Feb 2020
"So rather than creating an empty structure, why not create one with the fields that you need?"
These two things are not mutually exclusive: an empty structure can have any number of fields.
Walter Roberson
on 9 Feb 2020
An empty structure with a number of fields can be useful to force a particular order of fields ahead of time, as the default is the fields are in the order assigned (which is not necessarily the same even within one loop if it has conditions.) The order of the fields matters for structure concatenation [A, B] with the order having to be the same. The order of fields also matters for struct2cell interpretation.
Alexander Carmeli
on 4 Aug 2021
Try this:
output.(subnum)(1).name = hcp(x,1).name;
Note the subscript (1) after (subnum).
Think of a struct as a 1-element struct array. An empty struct is an array of length 0.
As others stated, if you know your field names, it's nicer to create the struct with those fields. However, if you do not know the field names at creation time, you can use this method to create an empty struct and dynamically add fields as you discover them.
s = struct([]) % How about s = struct.empty?
s.name = "hello"
% A dot name structure assignment is illegal when the structure is empty.
% Use a subscript on the structure
Now try:
s(1).name = "hello"
% s =
% struct with fields:
% name: "hello"
0 Comments
See Also
Categories
Find more on Structures 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!