MATLAB Answers

0

What should I use instead of cd?

Eva Balgova 님이 질문을 제출함. 10 Oct 2019
최근 활동 Stephen Cobeldick 님이 편집함. 10 Oct 2019
Hello,
I have a piece of code (see below) that works but needs optimising. Any advice on what should be used insted of cd? The fullfile command would not work. Any other tips and suggestions on how to optimise the code are welcome. Thanks!
%%% This script will acess the SPM.mat result files of individual subjects and save
%%% their activation maps as binary maps as well as rename and copy them to
%%% a new file. The script needs optimising.
clear;
basedir = 'D:\ToM_1_REANALYSED';
spm_dir = 'C:\spm12';
addpath(spm_dir);
Subjectids = {'ToM_001_dicom','ToM_002_dicom','ToM_004_dicom','ToM_005_dicom','ToM_006_dicom','ToM_007_dicom','ToM_008_dicom','ToM_010_dicom','ToM_011_dicom','ToM_013_dicom','ToM_014_dicom','ToM_016_dicom','ToM_017_dicom','ToM_018_dicom','ToM_019_dicom','ToM_021_dicom','ToM_022_dicom','ToM_024_dicom','ToM_025_dicom','ToM_026_dicom','ToM_027_dicom','ToM_028_dicom','ToM_029_dicom','ToM_030_dicom'};
data_dirs = {'ANALYSIS_PLANNED_REG_P001','ANALYSIS_PLANNED_REG_P002','ANALYSIS_PLANNED_REG_P004','ANALYSIS_PLANNED_REG_P005','ANALYSIS_PLANNED_REG_P006','ANALYSIS_PLANNED_REG_P007','ANALYSIS_PLANNED_REG_P008','ANALYSIS_PLANNED_REG_P010','ANALYSIS_PLANNED_REG_P011','ANALYSIS_PLANNED_REG_P013','ANALYSIS_PLANNED_REG_P014','ANALYSIS_PLANNED_REG_P016','ANALYSIS_PLANNED_REG_P017','ANALYSIS_PLANNED_REG_P018','ANALYSIS_PLANNED_REG_P019','ANALYSIS_PLANNED_REG_P021','ANALYSIS_PLANNED_REG_P022','ANALYSIS_PLANNED_REG_P024','ANALYSIS_PLANNED_REG_P025','ANALYSIS_PLANNED_REG_P026','ANALYSIS_PLANNED_REG_P027','ANALYSIS_PLANNED_REG_P028','ANALYSIS_PLANNED_REG_P029','ANALYSIS_PLANNED_REG_P030'};
%%%Matlab batch for saving binary maps from single subject results
for n=1:length(Subjectids)
cd ([basedir '\' Subjectids{n} '\' data_dirs{n} '\' 'TOM']) % works fine with cd, but try to think about better ways of doing this...
%%% fullfile ([basedir '\' Subjectids{n} '\' data_dirs{n} '\' 'TOM' '\'])
spm('defaults', 'FMRI');
spm_jobman('initcfg');
matlabbatch{1}.spm.stats.results.spmmat ={'SPM.mat'}
matlabbatch{1}.spm.stats.results.conspec.titlestr = '';
matlabbatch{1}.spm.stats.results.conspec.contrasts = 1;
matlabbatch{1}.spm.stats.results.conspec.threshdesc = 'none';
matlabbatch{1}.spm.stats.results.conspec.thresh = 0.001;
matlabbatch{1}.spm.stats.results.conspec.extent = 0;
matlabbatch{1}.spm.stats.results.conspec.conjunction = 1;
matlabbatch{1}.spm.stats.results.conspec.mask.none = 1;
matlabbatch{1}.spm.stats.results.units = 1;
matlabbatch{1}.spm.stats.results.export{1}.ps = true;
matlabbatch{1}.spm.stats.results.export{2}.binary.basename = '_binary_activation_map';
spm_jobman('run', matlabbatch);
movefile (['spmT_0001__binary_activation_map.nii'], [Subjectids{n} '_binary_activation_map.nii'])
copyfile([Subjectids{n} '_binary_activation_map.nii'],'\\fs-root-w\home-005\sepa70\Windows_Data\Desktop\PHD OFFICE\1_PROJECTS\TOM_TMS\TOM_peak_activations_from_fMRI_data\Single_Subject_Activation_Maps')
cd ../../../..
end

  댓글 수: 0

로그인 to comment.

답변 수: 2

thoughtGarden 님의 답변 10 Oct 2019

Your use of fullfile should be this:
fullfile(basedir, Subjectids{n}, data_dirs{n},'TOM')
Also, why are you trying to avoid using cd? cd is specifically designed to change folders, which it seems is your goal.
I suppose another option would be to copy the file to the desired folder using
copyfile
That way you aren't changing directories every loop.

  댓글 수: 4

표시 이전 댓글 수: 1
"why are you trying to avoid using cd? cd is specifically designed to change folders"
Using cd in code is not recommended, because:
  • it slows code down (on every folder changes MATLAB will scan the folder for any MATLAB-related files, which (because it requires OS file access) is not fast).
  • it changes function scoping, which can change which functions are called. This makes for very strange bugs which are difficult to track down.
  • it makes debugging much more difficult (due to the changing scope, and due to the inability to test file importing/existence/... using the input data).
There is absolutely no need to use cd to access data files: all MATLAB functions that import/export data files accept absolute/relative filenames. Using absolute/relative filenames is faster and much easier to debug.
For the same reason you should not call addpath for data directories (it serves no point, and only slows everything down).
Repeatedly adding directries to your search path will also slow down your code, and it will have a serious effect in that respect, since then MATLAB will probably decide to rehash the entire function cache.
As Stephen says, use fullfile properly, and there will be no need to cd to a directory, or to add directories to your searchpath.
Thank you for your answer. Could you please provide an example or amendment to the code that would lead to achieving this? Thanks. Eva

로그인 to comment.


Stephen Cobeldick 님의 답변 10 Oct 2019
Stephen Cobeldick 님이 편집함. 10 Oct 2019

Your fullfile usage (which you commented out) does not make much sense, in particular:
  1. concatenating everything together before calling fullfile and explicitly providing the path separator characters completely defeats the point of fullfile.
  2. You do not seem to allocate fullfile's output to anything, and hence do not use it anywhere in your code.
Instead of this:
% fullfile ([basedir '\' Subjectids{n} '\' data_dirs{n} '\' 'TOM' '\'])
you need something like this:
P = fullfile(basedir,Subjectids{n},data_dirs{n},'TOM');
and then you need to actually use P in your code. Possibly you want something like this:
destdir = '\\fs-root-w\home-005\sepa70\Windows_Data\Desktop\PHD OFFICE\1_PROJECTS\TOM_TMS\TOM_peak_activations_from_fMRI_data\Single_Subject_Activation_Maps';
basedir = 'D:\ToM_1_REANALYSED';
Subjectids = {'ToM_001_dicom','ToM_002_dicom','ToM_004_dicom','ToM_005_dicom','ToM_006_dicom','ToM_007_dicom','ToM_008_dicom','ToM_010_dicom','ToM_011_dicom','ToM_013_dicom','ToM_014_dicom','ToM_016_dicom','ToM_017_dicom','ToM_018_dicom','ToM_019_dicom','ToM_021_dicom','ToM_022_dicom','ToM_024_dicom','ToM_025_dicom','ToM_026_dicom','ToM_027_dicom','ToM_028_dicom','ToM_029_dicom','ToM_030_dicom'};
data_dirs = {'ANALYSIS_PLANNED_REG_P001','ANALYSIS_PLANNED_REG_P002','ANALYSIS_PLANNED_REG_P004','ANALYSIS_PLANNED_REG_P005','ANALYSIS_PLANNED_REG_P006','ANALYSIS_PLANNED_REG_P007','ANALYSIS_PLANNED_REG_P008','ANALYSIS_PLANNED_REG_P010','ANALYSIS_PLANNED_REG_P011','ANALYSIS_PLANNED_REG_P013','ANALYSIS_PLANNED_REG_P014','ANALYSIS_PLANNED_REG_P016','ANALYSIS_PLANNED_REG_P017','ANALYSIS_PLANNED_REG_P018','ANALYSIS_PLANNED_REG_P019','ANALYSIS_PLANNED_REG_P021','ANALYSIS_PLANNED_REG_P022','ANALYSIS_PLANNED_REG_P024','ANALYSIS_PLANNED_REG_P025','ANALYSIS_PLANNED_REG_P026','ANALYSIS_PLANNED_REG_P027','ANALYSIS_PLANNED_REG_P028','ANALYSIS_PLANNED_REG_P029','ANALYSIS_PLANNED_REG_P030'};
for n = 1 :numel(Subjectids)
P = fullfile(basedir,Subjectids{n},data_dirs{n},'TOM');
old = fullfile(P,'spmT_0001__binary_activation_map.nii');
new = fullfile(P,[Subjectids{n},'_binary_activation_map.nii']);
movefile(old,new)
copyfile(new,destdir)
end

  댓글 수: 0

로그인 to comment.



Translated by