Replacing a field in an structure that is subset within a larger structure
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Jen
el 2 de Feb. de 2024
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
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
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.Y = {1,2,3}
The fact that the structure is stored within another structure makes no difference.
Respuesta aceptada
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.event = rmfield(EEG.event, 'type')
[EEG.event.type] = codes{:}; % comma-separated list -> no error!
EEG.event.type % checking the elements of EVENT
Whereas your original approach throws an error:
EEG.event = rmfield(EEG.event, 'type');
EEG.event.type = codes; % this assignment will not work.
See also:
2 comentarios
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.Y = {1,2,3}
The error message could make that advice clearer, and give some advice on non-scalar structures too.
Más respuestas (0)
Ver también
Categorías
Más información sobre EEG/MEG/ECoG en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!