Changing variable names based on a string

5 views (last 30 days)
Hi all,
I am trying to do some basic imaging processing where I want a script to run through different ROIs on each hemisphere of the brain. Right now I have to run a script manually with each file name (RightPLIC). Is there anyway to code file names with two separate editable strings? i.e. ('hemisphere''ROItarget')
So that I don't have to manually change each line each time I run the code? In linux I typically use {$fname}, but that won't work here...
Thanks in advance for any help!
Below is an example of what I'm talking about. I want to be able to just update "right" or "M1" or "PLIC" more quickly.
% files needed to do pde tracking seedfile = [dirname, 'RightPLIC']; % seed targetfile = [dirname, 'RightM1_ACPC_DTI_space']; % target
maskfile = [dirname, 'dti.moco.bet '];
sidemaskfile = [dirname, 'right_mask']; % this will be combined with anatomical mask
% name of output file: nifti
outputfile = 'RightM1_ACPC.count';
File = 'RightM1_ACPC.count.nii.gz';

Accepted Answer

Nathan
Nathan on 13 Aug 2014
Edited: Nathan on 13 Aug 2014
Kelsey,
You could have a conditional block at the top of your code and you could change the value of say n and switch the value of the a string based on this.
n=1;
if n==1,
String1 = 'Right';
String2 = 'M1';
elseif n==2,
String = 'option2';
elseif n==3,
String = 'option3';
else
Error('Selection is not a valid option');
end
Then you can change your code in some form like:
% name of output file: nifti
outputfile = strcat(String1, String2, '_', 'ACPC.count');
File = strcat(String1, String2, '_', 'ACPC.count.nii.gz');
This method works great, just not sure if I percived exactly what parts needed to be dynamicallly changible. Let me know if you have any questions about this method.

More Answers (2)

Ahmet Cecen
Ahmet Cecen on 13 Aug 2014
Use eval alongside strcat and num2str.

Joseph Cheng
Joseph Cheng on 13 Aug 2014
Sightly confused on what you're trying to do but here is my attempt.
STR = 'Right'; %or M1 or PLIC
% files needed to do pde tracking
seedfile = [dirname, STR, 'PLIC']; % seed
targetfile = [dirname, STR, 'M1_ACPC_DTI_space']; % target
maskfile = [dirname, 'dti.moco.bet '];
sidemaskfile = [dirname, STR, '_mask']; % this will be combined with anatomical mask
% name of output file: nifti
outputfile = [STR, 'M1_ACPC.count'];
File = [STR, 'M1_ACPC.count.nii.gz';
Since these are all String arrays you can concatenate them with each other like you were already doing. Since i couldn't figure out based on your example of the target file and output file having RightM1 i left it in there. you could possibly edit this with a switch/case statement so depending on what the edit string is you can get two string substitution variables to make 'RightM1' instead of just right.
%air coding so pardon any errors
type = 'Right'
switch type
case 'right'
STR1 = 'Right'
STR2 = 'RightM1'
case 'M1'
STR1 = _)___
STR2=_____
case 'PLIC'
end
same stuff above but use STR1 or STR2 depending on which one fits.
Additionally if your files are named in a particular way you could write this to do it automatically and detect which strings to update.

Community Treasure Hunt

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

Start Hunting!