What should I use instead of cd?

7 visualizzazioni (ultimi 30 giorni)
Eva Balgova
Eva Balgova il 10 Ott 2019
Modificato: Stephen23 il 10 Ott 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

Risposte (2)

thoughtGarden
thoughtGarden il 10 Ott 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 Commenti
John D'Errico
John D'Errico il 10 Ott 2019
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.
Eva Balgova
Eva Balgova il 10 Ott 2019
Thank you for your answer. Could you please provide an example or amendment to the code that would lead to achieving this? Thanks. Eva

Accedi per commentare.


Stephen23
Stephen23 il 10 Ott 2019
Modificato: Stephen23 il 10 Ott 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

Categorie

Scopri di più su Search Path in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by