1

I need to use some toolbox in Matlab and haven't used Matlab in a long time. Wondering how to use a string variable for a placeholder when identifying a file in the pathway. For example in bash, ${SUBJID}/file1/abcd.nii, will the fullpath() in matlab works the same way, and how to save the output to the corresponding pathway?

a: A001/A001/file1/abcd.nii
b: A001/A001/file2/diff/mask.nii
output: A001/A001/file/output.txt    

bash:
export SUBJECTS_DIR=dir\
cd $SUBJECTS_DIR
SUBJID_LIST=[A001, A002, A003, B001, B002, ......]
for SUBJID in "${SUBJID_LIST[@]}"; do function 
-a ${SUBJID}/${SUBJID}/file1/abcd.nii 
-b ${SUBJID}/${SUBJID}/file2/diff/mask.nii 
-output ${SUBJID}/${SUBJID}/file/output; done

matlab:
SUBJID_LIST=[A001, A002, A003, B001, B002, ......]
for i in SUBJID_list
  path=dir\
  a=fullpath(path,i,abcd.nii) ??
  b=fullpath(path,i,mask.nii) ??
  output=function(a,b)
  writetable(output, "??? output.txt")
end 
2
  • With a bit of browsing, looks like you want mathworks.com/help/matlab/ref/plus.html Commented Apr 28, 2022 at 15:24
  • I am more concerned about using string variable to define the pathway not the matrix operation. Commented Apr 28, 2022 at 15:42

1 Answer 1

2

You could use fullfile to concatenate file paths and strrep to replace your placeholder with a variable

Note that your paths and names should be enclosed in quotes in MATLAB.

Also path is a build-in function name, so I'd advise avoiding it as a variable name.

SUBJID_LIST={'A001', 'A002', 'A003', 'B001', 'B002'};
myPath = 'dir\${SUBJID}\${SUBJID}\';
for i = 1:numel(SUBJID_LIST)
  iPath = strrep( myPath, '${SUBJID}', SUBJID_LIST{i} );
  a = fullfile(iPath, 'abcd.nii');
  b = fullfile(iPath, 'mask.nii');
  output = function(a,b);
  writetable(output, fullfile(iPath, 'output.txt');
end 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.