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
CodePudding user response:
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