Inserting fields into an empty structure

270 ビュー (過去 30 日間)
M K 2016 年 9 月 26 日
回答済み: Alexander Carmeli 2021 年 8 月 4 日
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:
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
%append new subject to existing subjects
disp([subnum comparison])
%loop through evry z cell and compare for evry xypair
for z=1:35778
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
And I just do not get what I need to do. I have gone through this page and this page , but it also did not work for me and I don't really understand what they're doing to boot. So can anyone help please?

回答 (3 件)

Walter Roberson
Walter Roberson 2016 年 9 月 26 日
  1 件のコメント
Raymond MacNeil
Raymond MacNeil 2020 年 2 月 9 日
Thanks, Walter.


Geoff Hayes
Geoff Hayes 2016 年 9 月 26 日
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('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 件のコメント
Walter Roberson
Walter Roberson 2020 年 2 月 9 日
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
Alexander Carmeli 2021 年 8 月 4 日
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? = "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"

Community Treasure Hunt

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

Start Hunting!

Translated by