<html dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2">
<style id="owaParaStyle" type="text/css">P {margin-top:0;margin-bottom:0;}</style>
</head>
<body ocsi="0" fpstyle="1">
<div style="direction: ltr;font-family: Tahoma;color: #000000;font-size: 10pt;">Thanks for your help Mikolaj and Makoto!<br>
<br>
I am very aware of the dangers of global variables, it is unfortunate that they seem so difficult to avoid in Matlab!<br>
<br>
I have created a version using evalin , here it is in case it comes in handy for someone. It is not fully tested, so user beware :)<br>
<br>
<font face="Courier New">function cleanedEEGData = detectArtefacts(eegData,threshold,slope,jp,dist,spec)<br>
%DETECTARTEFACTS Detects artefactual data using EEGLAB's functions, then<br>
%   displays the data in a plot for the user to check.<br>
%   Returns the eegData with artefactual trials removed. <br>
<br>
    %Absolute limit of electrode values in uV<br>
    if nargin<2<br>
        threshold=[];<br>
    end<br>
    lowerLimit=-150;<br>
    upperLimit=150;<br>
    <br>
    %Maximum slope over an epoch in uV/epoch<br>
    if nargin<3<br>
        slope=[];<br>
    end<br>
    maxSlope=60;<br>
    rVal=0.3;<br>
    <br>
    %Reject unlikely epochs, by number of SDs<br>
    if nargin<4<br>
        jp=[];<br>
    end<br>
    sdJPSingleChan=4;<br>
    sdJPAllChans=4;<br>
    <br>
    %Reject abnormal distributions, by number of SDs<br>
    if nargin<5<br>
        dist=[];<br>
    end<br>
    sdKurtSingleChan=4;<br>
    sdKurtAllChans=4;<br>
    <br>
    %Reject abnormal spectra<br>
    if nargin<6<br>
        spec=[];<br>
    end<br>
    thresholds=[-50 50;-100 25];<br>
    frequencies=[0 2;20 40];<br>
    <br>
    epochLength=eegData.xmax;<br>
    epochTimeFrames=eegData.pnts;<br>
    sampleRate=eegData.srate;<br>
    events=eegData.event;<br>
    nElec=eegData.nbchan;<br>
    <br>
    %Do the error detecting and get the format for marking trials in<br>
    %eegplot<br>
    if ~isempty(threshold)<br>
        eegData=pop_eegthresh(eegData,1,1:nElec,lowerLimit,upperLimit,0,epochLength,1,0,0);<br>
        plotRejThr=trial2eegplot(eegData.reject.rejthresh,eegData.reject.rejthreshE,epochTimeFrames,eegData.reject.rejthreshcol);<br>
        eegData.comments = pop_comments(eegData.comments,'',['Filtered by pop_eegthresh, lowerLimit = ' num2str(lowerLimit) ',upperLimit = ' num2str(upperLimit)],1);
<br>
   else<br>
        plotRejThr=[];<br>
    end<br>
    if ~isempty(slope)<br>
        eegData=pop_rejtrend(eegData,1,1:nElec,epochTimeFrames,maxSlope,rVal,1,0,0);<br>
        plotRejTre=trial2eegplot(eegData.reject.rejconst,eegData.reject.rejconstE,epochTimeFrames,eegData.reject.rejconstcol);<br>
        eegData.comments = pop_comments(eegData.comments,'',['Filtered by pop_rejtrend, maxSlope = ' num2str(maxSlope)],1);
<br>
    else<br>
        plotRejTre=[];<br>
    end<br>
    if ~isempty(jp)<br>
        eegData=pop_jointprob(eegData,1,1:nElec,sdJPSingleChan,sdJPAllChans,1,0);<br>
        plotRejJp =trial2eegplot(eegData.reject.rejjp,eegData.reject.rejjpE,epochTimeFrames,eegData.reject.rejjpcol);<br>
        eegData.comments = pop_comments(eegData.comments,'',['Filtered by pop_jointprob, sdJPSingleChan = ' num2str(sdJPSingleChan) ',sdJPAllChans = ' num2str(sdJPAllChans)],1);
<br>
    else<br>
        plotRejJp=[];<br>
    end<br>
    if ~isempty(dist)<br>
        eegData=pop_rejkurt(eegData,1,1:nElec,sdKurtSingleChan,sdKurtAllChans,1,0,1);<br>
        plotRejKur=trial2eegplot(eegData.reject.rejkurt,eegData.reject.rejkurtE,epochTimeFrames,eegData.reject.rejkurtcol);<br>
        eegData.comments = pop_comments(eegData.comments,'',['Filtered by pop_rejkurt, sdKurtSingleChan = ' num2str(sdKurtSingleChan) ',sdKurtAllChans = ' num2str(sdKurtAllChans)],1);
<br>
    else<br>
        plotRejKur=[];<br>
    end<br>
    if ~isempty(spec)<br>
        eegData=pop_rejspec( eegData, 1,'elecrange',1:30,'threshold',thresholds,'freqlimits',frequencies,'eegplotcom','','eegplotplotallrej',1,'eegplotreject',0,'specdata',eegData.specdata);<br>
        plotRejSpe=trial2eegplot(eegData.reject.rejfreq,eegData.reject.rejfreqE,epochTimeFrames,eegData.reject.rejfreqcol);<br>
        eegData.comments = pop_comments(eegData.comments,'',['Filtered by pop_rejkurt, threshold = ' num2str(thresholds) ',freqlimits = ' num2str(frequencies)],1);
<br>
    else<br>
        plotRejSpe=[];<br>
    end<br>
    assignin('base', 'EEG', eegData);<br>
    evalin('base','detectArtefactsFinished=0;');<br>
    <br>
    %command string for reject marked trials... all marked epochs...<br>
    %adapted from http://sccn.ucsd.edu/pipermail/eeglablist/2011/004085.html<br>
    cmd = [ ...<br>
    '[tmprej tmprejE] = eegplot2trialMod( TMPREJ,' num2str(eegData.pnts) ',' num2str(eegData.trials) ');' ...<br>
    'EEG = pop_rejepoch(EEG, tmprej, 1);' ...<br>
    'detectArtefactsFinished=1;' ...<br>
    ] ;<br>
<br>
    rejE=[plotRejThr;plotRejTre;plotRejJp,plotRejKur,plotRejSpe];<br>
    %Draw the data.<br>
    eegplot(eegData.data,...<br>
        'srate',sampleRate,...<br>
        'events',events,...<br>
        'winrej',rejE,...<br>
        'command',cmd,...<br>
        'butlabel','Reject');<br>
    <br>
    %Wait until the user has finished reviewing.<br>
    reviewFinished=0;<br>
    while ~reviewFinished<br>
        reviewFinished=evalin('base','detectArtefactsFinished');<br>
        pause(0.01);<br>
    end<br>
    evalin('base','clear detectArtefactsFinished');<br>
   <br>
    cleanedEEGData=evalin('base','EEG');<br>
end<br>
<br>
<br>
</font>
<div style="font-family: Times New Roman; color: #000000; font-size: 16px">
<hr tabindex="-1">
<div style="direction: ltr;" id="divRpF26463"><font color="#000000" face="Tahoma" size="2"><b>From:</b> Makoto Miyakoshi [mmiyakoshi@ucsd.edu]<br>
<b>Sent:</b> Tuesday, July 01, 2014 6:49 AM<br>
<b>To:</b> Mikołaj Magnuski<br>
<b>Cc:</b> EEGLAB List; Matthew Moore<br>
<b>Subject:</b> Re: [Eeglablist] Using the 'command' field of eeg_plot to reject trials<br>
</font><br>
</div>
<div></div>
<div>
<div dir="ltr">Very good to know, thanks!
<div><br>
</div>
<div>Makoto</div>
</div>
<div class="gmail_extra"><br>
<br>
<div class="gmail_quote">On Mon, Jun 30, 2014 at 11:23 AM, Mikołaj Magnuski <span dir="ltr">
<<a href="mailto:imponderabilion@gmail.com" target="_blank">imponderabilion@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex; border-left:1px #ccc solid; padding-left:1ex">
<p dir="ltr">It depends on a number of things. For example some of the eeglab functions that you call may be declaring EEG as global or evaluating it in workspace (but in such case this should happen whatever name you gave to the variable passed as EEG).<br>
In some cases it may be safer to declare EEG as global / evalin yourself. I remember once having a function that kept working on the same file instead of  looping through multiple files. The global declaration in one of the eeglab functions was overwriting
 the EEG that it got passed with the previously global EEG (the first EEG processed).<br>
To avoid these perils one can declare EEG as global at the relevant level, use evalin('base', 'EEG', EEG) or clearvars -global EEG :)<br>
</p>
<div class="gmail_quote">30 cze 2014 19:01 "Makoto Miyakoshi" <<a href="mailto:mmiyakoshi@ucsd.edu" target="_blank">mmiyakoshi@ucsd.edu</a>> napisał(a):
<div>
<div class="h5"><br type="attribution">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex; border-left:1px #ccc solid; padding-left:1ex">
<div dir="ltr">Thanks MIkolaj. Actually I did not know it. It seems it has been automatically taken care of in my case probably because I have launched EEGLAB before using any of eeglab functions...
<div><br>
</div>
<div>Makoto</div>
</div>
<div class="gmail_extra"><br>
<br>
<div class="gmail_quote">On Sat, Jun 28, 2014 at 12:15 AM, Mikołaj Magnuski <span dir="ltr">
<<a href="mailto:imponderabilion@gmail.com" target="_blank">imponderabilion@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex; border-left:1px #ccc solid; padding-left:1ex">
<p dir="ltr">Dear Matthew and Makoto,</p>
<p dir="ltr">synchronizing with base workspace eeglab GUI usually requires a bit more (unless you want to change your function into a script).<br>
The easiest way is to declare relevant eeglab variables as globals at the beginning of the function (EEG, ALLEEG, etc. - whatever eeglab variables you are using and want to sync with workspace gui). For example:<br>
global EEG</p>
<p dir="ltr">Unfortunately, using globals is usually frowned upon by programmers (for good reasons) but I don't think you should worry too much when using globals in small applications.</p>
<p dir="ltr">Another way however would be to assign (see assignin docummentation) relevant eeglab variables in the base workspace and evaluate (see evalin) eeglab GUI commands in the base workspace too.</p>
<p dir="ltr">Try globals first. EEGlab declares most (if not all) of the all-caps base workspace variables as globals and synchronizes the gui functions (at least some of them) with the workspace this way - so you should be fine with global variables (from
 my experience - using globals is the easiest and most efficient way of creating and synchronizing GUI data in matlab without blocking the command window with uiwait( ) etc. ).</p>
<div class="gmail_quote">28 cze 2014 05:35 "Makoto Miyakoshi" <<a href="mailto:mmiyakoshi@ucsd.edu" target="_blank">mmiyakoshi@ucsd.edu</a>> napisał(a):
<div>
<div><br type="attribution">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex; border-left:1px #ccc solid; padding-left:1ex">
<div dir="ltr">Dear Matthew,
<div><br>
</div>
<div>Looks like you want to automate the epoch rejection process.</div>
<div><br>
</div>
<div><span style="color:rgb(0,0,0); font-family:Tahoma; font-size:13.142857551574707px">> My problem may be that I can't seem to find how to load the eeglab workspace variables, other than through the gui.</span><br>
</div>
<div><span style="color:rgb(0,0,0); font-family:Tahoma; font-size:13.142857551574707px"><br>
</span></div>
<div>I recommend you replace all eegData in your code to EEG for the sake of simplicity. As long as they are 'EEG' by typing 'eeglab redraw' you can interact with it through EEGLAB main GUI.</div>
<div><br>
</div>
<div>To load data, use EEG = pop_loadset()... of course you can do eegData = pop_loadset()... but again using 'EEG' allows you to interact it via GUI.</div>
<div><br>
</div>
<div>Makoto</div>
</div>
<div class="gmail_extra"><br>
<br>
<div class="gmail_quote">On Tue, Jun 24, 2014 at 9:44 PM, Matthew Moore <span dir="ltr">
<<a href="mailto:matthew.moore@otago.ac.nz" target="_blank">matthew.moore@otago.ac.nz</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex; border-left:1px #ccc solid; padding-left:1ex">
<div>
<div style="direction:ltr; font-family:Tahoma; color:#000000; font-size:10pt">Hi,<br>
<br>
I am having a bit of trouble getting eegplot to allow inspection and editing of candidates for rejection.<br>
<br>
I have looked at this thread: <a href="http://sccn.ucsd.edu/pipermail/eeglablist/2011/004085.html" target="_blank">
http://sccn.ucsd.edu/pipermail/eeglablist/2011/004085.html</a><br>
<br>
I tried to implement the command listed there, but am having trouble. My problem may be that I can't seem to find how to load the eeglab workspace variables, other than through the gui.<br>
<br>
I would really appreciate if someone could have a look at the code and give me some pointers.<br>
<br>
Matt<br>
<font face="Courier New"><br>
function cleanedEEGData = detectArtefacts(eegData,locFile)<br>
%DETECTARTEFACTS Detects artefactual data using EEGLAB's functions, then<br>
%   displays the data in a plot for the user to check.<br>
%   Returns the eegData with artefactual trials removed.<br>
%   locFile is the file containing electrode locations.<br>
    <br>
    epochLength=eegData.xmax;<br>
    epochTimeFrames=eegData.pnts;<br>
    sampleRate=eegData.srate;<br>
    events=eegData.event;<br>
    nElec=eegData.nbchan;<br>
    <br>
    %Absolute limit of electrode values in uV<br>
    lowerLimit=-150;<br>
    upperLimit=150;<br>
    <br>
    %Maximum slope over an epoch in uV/epoch<br>
    maxSlope=60;<br>
    rVal=0.3;<br>
    <br>
    %Reject unlikely epochs, by number of SDs<br>
    sdJPSingleChan=4;<br>
    sdJPAllChans=4;<br>
    <br>
    %Reject abnormal distributions, by number of SDs<br>
    sdKurtSingleChan=4;<br>
    sdKurtAllChans=4;<br>
    <br>
    %Reject abnormal spectra<br>
    thresholds=[-50 50;-100 25];<br>
    frequencies=[0 2;20 40];<br>
    <br>
    %Do the error detecting.<br>
    eegData=pop_eegthresh(eegData,1,1:nElec,lowerLimit,upperLimit,0,epochLength,1,0,0);<br>
    eegData=pop_rejtrend(eegData,1,1:nElec,epochTimeFrames,maxSlope,rVal,1,0,0);<br>
    eegData=pop_jointprob(eegData,1,1:nElec,sdJPSingleChan,sdJPAllChans,1,0);<br>
    eegData=pop_rejkurt(eegData,1,1:nElec,sdKurtSingleChan,sdKurtAllChans,1,0);<br>
%     eegData=pop_rejspec( eegData, 1,'elecrange',1:30,'threshold',thresholds,'freqlimits',frequencies,'eegplotcom','','eegplotplotallrej',1,'eegplotreject',0,'specdata',eegData.specdata);<br>
<br>
    %Get the format for marking trials in an eegplot<br>
    plotRejThr=trial2eegplot(eegData.reject.rejthresh,eegData.reject.rejthreshE,epochTimeFrames,eegData.reject.rejthreshcol);<br>
    plotRejTre=trial2eegplot(eegData.reject.rejconst,eegData.reject.rejconstE,epochTimeFrames,eegData.reject.rejconstcol);<br>
    plotRejJp =trial2eegplot(eegData.reject.rejjp,eegData.reject.rejjpE,epochTimeFrames,eegData.reject.rejjpcol);<br>
    plotRejKur=trial2eegplot(eegData.reject.rejkurt,eegData.reject.rejkurtE,epochTimeFrames,eegData.reject.rejkurtcol);<br>
%     rejSpe=trial2eegplot(eegData.reject.rejfreq,eegData.reject.rejfreqE,epochTimeFrames,eegData.reject.rejfreqcol);<br>
    <br>
    %Put the reject candidates in one array.<br>
    rejE=[plotRejThr;plotRejTre;plotRejJp;plotRejKur];%;rejSpe];<br>
    <br>
    %command string for reject marked trials... all marked epochs...<br>
    %from <a href="http://sccn.ucsd.edu/pipermail/eeglablist/2011/004085.html" target="_blank">
http://sccn.ucsd.edu/pipermail/eeglablist/2011/004085.html</a><br>
    cmd = [ ...<br>
    '[tmprej tmprejE] = eegplot2trial( TMPREJ,EEG.pnts,EEG.trials);' ...<br>
    '[EEGTMP LASTCOM] = pop_rejepoch(EEG, tmprej, 1);' ...<br>
    'if ~isempty(LASTCOM),'...<br>
    ' [ALLEEG EEG CURRENTSET tmpcom] = pop_newset(ALLEEG, EEGTMP, CURRENTSET);' ...<br>
    ' if ~isempty(tmpcom),' ...<br>
    '  EEG = eegh(LASTCOM, EEG);' ...<br>
    '  eegh(tmpcom);' ...<br>
    '  eeglab(''redraw'');' ...<br>
    ' end;' ...<br>
    'end;' ...<br>
    'clear EEGTMP tmpcom;' ...<br>
    ] ;<br>
<br>
    %Draw the data.<br>
    eegplot(eegData.data,...<br>
        'eloc_file',locFile,...<br>
        'srate',sampleRate,...<br>
        'events',events,...<br>
        'winrej',rejE,...<br>
        'command',cmd,...<br>
        'butlabel','Reject');<br>
<br>
<br>
end</font><br>
<br>
</div>
</div>
<br>
_______________________________________________<br>
Eeglablist page: <a href="http://sccn.ucsd.edu/eeglab/eeglabmail.html" target="_blank">
http://sccn.ucsd.edu/eeglab/eeglabmail.html</a><br>
To unsubscribe, send an empty email to <a href="mailto:eeglablist-unsubscribe@sccn.ucsd.edu" target="_blank">
eeglablist-unsubscribe@sccn.ucsd.edu</a><br>
For digest mode, send an email with the subject "set digest mime" to <a href="mailto:eeglablist-request@sccn.ucsd.edu" target="_blank">
eeglablist-request@sccn.ucsd.edu</a><br>
</blockquote>
</div>
<br>
<br clear="all">
<div><br>
</div>
-- <br>
<div dir="ltr">Makoto Miyakoshi<br>
Swartz Center for Computational Neuroscience<br>
Institute for Neural Computation, University of California San Diego<br>
</div>
</div>
<br>
_______________________________________________<br>
Eeglablist page: <a href="http://sccn.ucsd.edu/eeglab/eeglabmail.html" target="_blank">
http://sccn.ucsd.edu/eeglab/eeglabmail.html</a><br>
To unsubscribe, send an empty email to <a href="mailto:eeglablist-unsubscribe@sccn.ucsd.edu" target="_blank">
eeglablist-unsubscribe@sccn.ucsd.edu</a><br>
For digest mode, send an email with the subject "set digest mime" to <a href="mailto:eeglablist-request@sccn.ucsd.edu" target="_blank">
eeglablist-request@sccn.ucsd.edu</a><br>
</blockquote>
</div>
</div>
</div>
</blockquote>
</div>
<br>
<br clear="all">
<div><br>
</div>
-- <br>
<div dir="ltr">Makoto Miyakoshi<br>
Swartz Center for Computational Neuroscience<br>
Institute for Neural Computation, University of California San Diego<br>
</div>
</div>
</blockquote>
</div>
</div>
</div>
</blockquote>
</div>
<br>
<br clear="all">
<div><br>
</div>
-- <br>
<div dir="ltr">Makoto Miyakoshi<br>
Swartz Center for Computational Neuroscience<br>
Institute for Neural Computation, University of California San Diego<br>
</div>
</div>
</div>
</div>
</div>
</body>
</html>