Advanced String Swapping

This section elaborates on the many things one can do with the sting swapping methods used in this plug in. String swapping is used to quickly edit configuration files and history template scripts. The function that preforms the work is Key String Swap (key_strswap) and you can find its help text below for more information.

Example 1

The most basic string swap is a direct swap

  • Config contains, fav_color,'red'

  • Script has, disp(['my favourite color is: ' [fav_color] ]);

  • Script generated to run is, disp(['my favourite color is: ' 'red' ]);

This also works with

  • [batch_dfn] - Batch Data File Name
  • [batch_hfn] - Batch Histroy File Name
  • [batch_dfp] - Batch Data File Path

And you do not have to specify these in any config as they are gathered from the file loading section instead.

Example 2

This function also lets you use a shorthand for modifing these output as well, for example

  • File Name is , Subject_1_John_Doe_GoNoGoTask.set

  • Script has, disp('Saving file: [batch_dfn,.,-1]_sa_purge.set... ]);

  • Script generated to run is, disp('Saving file: Subject_1_John_Doe_GoNoGoTask_sa_purge.set... ]);

As you can see the swap string was not directly replaced. The extra arguments inside the square brackets indicated to

  • start from the end moving left (negative number)
  • go to the first period ( the . character in as the second argument and the number 1)
  • remove all of the characters including this one till the end

This got rid of the file extension allowing the _sa_purge.set to fit nicely to the end of the new file name.

Example 3

Using this schema you can make any number of alterations to the strings you are swapping in. This is very helpful for naming jobs, for example if we wanted to remove the name of the participant from the file name and add the script name that is executing we could

  • File Name is , Subject_1_John_Doe_GoNoGoTask.set

  • Script Name is , s01_scalpart.m

  • Config needs , job_name, [batch_hfn,.,1]_[batch_dfn,_,-3]_[_,4,batch_dfn]

  • Behind the Scenes, [s01_scalpart .m] _ [Subject_1 _John_Doe_GoNoGoTask.set] _ [Subject_1_John_Doe_ GoNoGoTask.set]

  • Jobname/Script name generated to run is, s01_scalpart_Subject_1_GoNoGoTask.set

This is expecially helpful running batches on files with long names, matlab will only run scripts with names less then 64 characters. For file names such as eeg_RSVP_Study_session_1_task_main_subjectLabId_10_1_KMM_recording1.bdf running a script named after the subject would not be possible without cutting out parts first. As the important information is the session/subject number you could use the same technique as above to cut out everything else after that using a ...,_,-9].


key_strswap() - Replace any instance of a key string surrounded by brackets (e.g. [keystr]) in a given string array with its key value swap string.

Usage: instr=key_strswap(instr,keystr,swapstr)

Required Inputs:

  • instr = A string array that contains instances of a key string that are to be replaced with key value swap strings.
  • keystr = A string that can be found in the instr surrounded by brackets (e.g. [fname]) and that is to be replaced with the characters in swapstr.
  • swapstr = A string that will replace any instance of the keystr (surronded by brackets) in the instr.


  • instr = Updated instr with instances of keystr replaced swapst

Notes: key_strswap is capable of replacing portions of the keystr instances in instr by adding comma separated options within the bracketed key strings in the instring text. In its simplest form key_strswap replaces all instances of keystr in instr with swapstr. For example, given:

  • instr ='The name of the input file is [infname]'
  • keystr ='infname'
  • swapstr ='ABC_s01_task1_raw.set'

... instr=key_strswap(instr,keystr,swapstr); would result in: instr = 'The name of the input file is ABC_s01_task1_raw.set'

By specifying specific swapstr characters within instr it is also possible to manipulate how the swapstr ends up being expressed in the resulting instr. This can be valuable when generating an ourput filename given an input file name in swapstr. For example, if we want to truncate the beginning of the infname and change the suffix of the infname when generating the output file name we could modify instr such that:

  • instr ='The name of the output file is [,1,infname,,-1]_out.set'

... instr=key_strswap(instr,keystr,swapstr); would result in: instr = 'The name of the output file is s01_task1_out.set'

The optional character and index options in the instr bracketed key string text determine what portions of the swapstr gets placed into the instr. Starting with "[,1...", this cuts off the beginning of infname (because these optional characters preceed infname within the brackets) at (and including) the first "" character from the left ("1" means first from the left). Then "...infname,,-1]" cuts off the training part of infname (because these optional characters follow infname within the brackets) starting (and including) the final "" character ("-1" indicates first from the right) in swapstr. Because key_strswap only replaces the keystr instances up-to and including the bracket, The "..]_out.set" ends up being appended to the end of the keystr in the resulting instr.

:house: Return to Main Page