How do you write a script to create new events that requires multiple conditions?

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)

You cannot use or like that. Probably ismember does what you want:
ismember({sRaw.F.events.label}, {'50','52'})

10 Comments

That still is coming up with the same error.
Hmmm....
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
Okay, so by doing that it ended up executing, however it executed only for the "52" codes, not the 50.
Try to make a MWE so we can run your code without any other dependencies and can reproduce your issue.
sRaw = in_bst_data(RawFile, 'F');
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
iEvtNew = length(sRaw.F.events) + 1;
sRaw.F.events(iEvtNew) = sRaw.F.events(iEvtCorr);
bst_save(file_fullpath(RawFile), sRaw, 'v6', 1);
It appears the issue must be within the "otherwise" component, since 52 is being accepted.
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
I do not understand what you mean by format my code.
RawFile = 'C:\Users\Rex\.brainstorm\MONSTER_Dataset\data\S204\@raw204_clean\data_0raw_204_clean.mat';
sRaw = in_bst_data(RawFile, 'F');
iEvtCorr = find(ismember({sRaw.F.events.label}, {'50','52'}));
switch numel(iEvtCorr)
case 0
error('no match found')
case 1
otherwise
error('too many matches found')
end
iEvtNew = length(sRaw.F.events) + 1;
sRaw.F.events(iEvtNew) = sRaw.F.events(iEvtCorr);
bst_save(file_fullpath(RawFile), sRaw, 'v6', 1);
Like that?
Also, I ran the code and it came out as:
"
Error using test (line 17)
too many matches found
"
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.
I see.
So in the program 'brainstorm', there is a function in which event codes (the '50' and '52') store lists of time markers. There is a function in the gui to merge the lists, however, it is manual and cannot be applied to all of the sets. I am trying to create a script that will find the '50' and '52' list, duplicate them, and then merge. This script copies and pastes (1) of the lists, but it seems it cannot merge the two.
So that is the goal.
I am not sure what you mean by scalar -- is that the same as intiger?
"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.

Sign in to comment.

Categories

Asked:

on 12 Apr 2020

Commented:

on 13 Apr 2020

Community Treasure Hunt

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

Start Hunting!