Azzera filtri
Azzera filtri

Info

Questa domanda è chiusa. Riaprila per modificarla o per rispondere.

syntax question-- how to reference a file based on same starting file name?

1 visualizzazione (ultimi 30 giorni)
Hi all, I am writing a batch file for SPM, but this question should be applicable to anyone who is better at MatLab syntax than I am...
I am getting an error that spm_jobman cannot find the file I am referencing:
1.) Earlier in my script, I made a variable "address":
address = ['M:/Flight/02_Nifti/',subjID,'/',timepoint];
I did this because of our file organization. Each person's data is within a unique subjID folder (with unique #'s such as 5000, etc.). Then, within that subjID folder, each person has multiple timepoint folders (01,02,03, etc.)
2.) Later in my script, I want to reference a file that starts with "mean" found the folder 04_VEMP, which is within each person's timepoint folders. For example, for one person with subjID 5000 and timepoint 01, the full path might be: M:/Flight/02_Nifti/5000/01/04_VEMP/mean_johnsmith_taskbased.nii
The tricky part is that each subject's name is a part of the file name under 04_VEMP. I am not able to change the file names, so how can I make a general statement that will allow me to always reference that file that starts with "mean" but has a different ending for each person? When I am cd'd into the folder containing these files, if I do ls mean*.nii or ls mean*, this returns only the mean file I am interested in. However, I am not sure how to do this same type of thing when referencing a file path in MatLab.
I have tried and gotten errors with:
fprintf(fID,['matlabbatch{1}.spm.spatial.coreg.estimate.ref = {''',[address,'/04_VEMP/mean*.nii,1''};'],'\n']);
fprintf(fID,['matlabbatch{1}.spm.spatial.coreg.estimate.ref = {''',[address,'/04_VEMP/','mean*','1''};'],'\n']);
I think the ,1 is something that I need in this case for SPM, but I am not positive.
Thanks!

Risposte (1)

Rik
Rik il 5 Ott 2017
Modificato: Walter Roberson il 5 Ott 2017
Matlab helps you a lot with data type conversion, but it doesn't go all the way. You tried to concatenate a numeric value with a string. Matlab has no way of knowing how it should do that conversion, so it tries to simply cast the data to char. This means that a numeric value 32 is converted to a space, 65 to uppercase A, and so on. To do this correctly you can either use num2str, or use sprintf.
address = sprintf('M:/Flight/02_Nifti/%d/%d',subjID,timepoint);
As for your second question: Matlab can only read one file at a time with this method, so you need to search for the file with dir and then use fopen to generate a fID for the file you want to write to.
list_of_files=dir([address,'/04_VEMP/mean*.nii']);
fID=fopen([address,'/04_VEMP/' list_of_files(1).name],'w');
Your question is not really easy to understand, which makes it difficult to parse the Matlab part of this question. (I am ignoring the rest, as it doesn't ring a bell) For future reference: have a read here and here. It will greatly improve your chances of getting an answer.

Questa domanda è chiusa.

Community Treasure Hunt

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

Start Hunting!

Translated by