Replacing a field in an structure that is subset within a larger structure

3 visualizaciones (últimos 30 días)
I'm currently trying to resolve a scripting issue with EEG data, whereby I want to replace a field (type) within a structure (event) that is part of a larger structure (i.e., EEG.event.type).
The replacement data is the same length as EEG.event.type, and is currently stored as type cell, called 'codes'.
At present my code looks like this:
EEG.event = rmfield(EEG.event, 'type') %remove field 'type' from EEG.event structure
EEG.event.type = codes;
this does not work, and instead gives me the error 'Scalar structure required for this assignment.'
However, if I try to add codes to the structure EEG, this seem to work using identical code:
EEG.new = codes;
So specifically, I seem to be having an issue with creating a new field in a structure that is subset within another structure.
Any input very gratefully recieved!
  2 comentarios
Jon
Jon el 2 de Feb. de 2024
Could you please attach a small, self contained runnable example that reproduces the problem, so we can see exactly what is going wrong
Stephen23
Stephen23 el 2 de Feb. de 2024
Editada: Stephen23 el 2 de Feb. de 2024
"I seem to be having an issue with creating a new field in a structure that is subset within another structure"
No, it is entirely due to the size of the structure that you are trying to assign to:
S = struct('X',{1,2,3})
S = 1×3 struct array with fields:
X
S.Y = {1,2,3}
Scalar structure required for this assignment.
The fact that the structure is stored within another structure makes no difference.

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 2 de Feb. de 2024
Editada: Stephen23 el 2 de Feb. de 2024
If EVENT is non-scalar them you will get that error. The correct approach is to use a comma-separated list.
Lets try it right now:
codes = {7,8,9};
EEG = struct('event', struct('type',{1,2,3}, 'blah',{4,5,6}))
EEG = struct with fields:
event: [1×3 struct]
EEG.event = rmfield(EEG.event, 'type')
EEG = struct with fields:
event: [1×3 struct]
[EEG.event.type] = codes{:}; % comma-separated list -> no error!
EEG.event.type % checking the elements of EVENT
ans = 7
ans = 8
ans = 9
Whereas your original approach throws an error:
EEG.event = rmfield(EEG.event, 'type');
EEG.event.type = codes; % this assignment will not work.
Scalar structure required for this assignment.
See also:
  2 comentarios
Jen
Jen el 5 de Feb. de 2024
Thank you so much Stephen, that's done it! I had no idea as to the correct approach for the scalar structure error but this makes total sense now.
Thanks again!
Stephen23
Stephen23 el 5 de Feb. de 2024
Editada: Stephen23 el 5 de Feb. de 2024
"I had no idea as to the correct approach for the scalar structure error..."
Yes, that message is not very clear. As far as I can tell, the "scalar structure" refers to what should be on the LHS of the assignment, in order for that assignment to work:
S = struct('X',pi) % scalar structure
S = struct with fields:
X: 3.1416
S.Y = {1,2,3}
S = struct with fields:
X: 3.1416 Y: {[1] [2] [3]}
The error message could make that advice clearer, and give some advice on non-scalar structures too.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre EEG/MEG/ECoG en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by