Changing variable names based on a string

조회 수: 2 (최근 30일)
Kelsey
Kelsey 2014년 8월 13일
댓글: Kelsey 2014년 8월 13일
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';

채택된 답변

Nathan
Nathan 2014년 8월 13일
편집: Nathan 2014년 8월 13일
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.
  댓글 수: 1
Kelsey
Kelsey 2014년 8월 13일
Thank you so much Nathan! This worked beautifully!

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Ahmet Cecen
Ahmet Cecen 2014년 8월 13일
Use eval alongside strcat and num2str.

Joseph Cheng
Joseph Cheng 2014년 8월 13일
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.

카테고리

Help CenterFile Exchange에서 Get Started with Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by