How do you write a script to create new events that requires multiple conditions?
Show older comments
Good afternoon,
Based on the very helpful tutorial at https://neuroimage.usc.edu/brainstorm/Tutorials/Scripting#Example:_Editing_events I have gotten quite far into my event-coding script, however, I have reached a snag.
I am trying to create a new event which combines two (or up to 10 events). I figured that an OR statement was necessary, however it appears that when adding the or statement, I tripped up the script.
% Find the index of the event category "button"
iEvtCorr = find(strcmpi({sRaw.F.events.label}, or('50','52')));
% Copy the event category "button" to a new category
iEvtNew = length(sRaw.F.events) + 1;
sRaw.F.events(iEvtNew) = sRaw.F.events(iEvtCorr);
% Rename the new event to "button_offset"
sRaw.F.events(iEvtNew).label = 'Correct3';
nTimes = size(sRaw.F.events(iEvtNew).times, 2);
sRaw.F.events(iEvtNew).epochs = ones(1, nTimes);
sRaw.F.events(iEvtNew).channels = cell(1, nTimes);
sRaw.F.events(iEvtNew).notes = cell(1, nTimes);
% Change the event color to yellow (red=1, green=1, blue=0)
sRaw.F.events(iEvtNew).color = [1 1 0];
% Update the sRaw structure to the RawFile file (the last parameter appends to the existing struct)
bst_save(file_fullpath(RawFile), sRaw, 'v6', 1);
This is what I have.
The bolded section is where I tripped up the program. It keeps assigning the error:
Unable to perform assignment because the left and right sides have a different number of elements.
Error in script_new (line 14)
sRaw.F.events(iEvtNew) = sRaw.F.events(iEvtCorr);
Any advice would be well received.
Thanks!
Answers (1)
Stephen23
on 12 Apr 2020
You cannot use or like that. Probably ismember does what you want:
ismember({sRaw.F.events.label}, {'50','52'})
10 Comments
Nicholas Surdel
on 12 Apr 2020
Rik
on 12 Apr 2020
Did you use this line of code?
iEvtCorr = find(ismember({sRaw.F.events.label}, {'50','52'}));
That must return a scalar, otherwise your line below has a mismatch in the number of elements.
sRaw.F.events(iEvtNew) = sRaw.F.events(iEvtCorr);
If this is not the case, you need to write code that will either throw an error, or makes sure this condition holds. For example:
iEvtCorr = find(ismember({sRaw.F.events.label}, {'50','52'}));
switch numel(iEvtCorr)
case 0
%empty, no matches
error('no match found')
case 1
%keep as is
otherwise
%more than one location, use the last
iEvtCorr=iEvtCorr(end);
end
Nicholas Surdel
on 12 Apr 2020
Nicholas Surdel
on 12 Apr 2020
Edited: Rik
on 12 Apr 2020
Rik
on 12 Apr 2020
You forgot to attach the required data to run your code. You also forgot to format your code.
The issue is not in the otherwise clause. The issue is that your later code expects the iEvtCorr variable to be a scalar (because iEvtNew is a scalar by definition). This switch block ensures that, but if it doesn't make sense for your case, you could change it to this:
iEvtCorr = find(ismember({sRaw.F.events.label}, {'50','52'}));
switch numel(iEvtCorr)
case 0
error('no match found')
case 1
%keep as is
otherwise
error('too many matches found')
end
Nicholas Surdel
on 12 Apr 2020
Rik
on 12 Apr 2020
Yes, that is what I mean by formatting your code.
Of course you get this error. You were the one telling the otherwise branch was run. I really don't know how I can say it in a different way. That variable has to be a scalar. It is not a scalar. You need to fix that. If you want help fixing that you will have to explain what your code should be doing.
Nicholas Surdel
on 12 Apr 2020
Stephen23
on 13 Apr 2020
"I am not sure what you mean by scalar ..."
A scalar is an array with one element, i.e. it is an array with size 1x1(x1x1x1x1...).
"... is that the same as intiger?"
No.
A scalar can be integer, floating point, character, cell, structure, etc., although in this case only the numeric ones are relevant. The class of a data type (e.g. integer types) does not tell you its array size.
Categories
Find more on Multidimensional Arrays 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!