A.Rainer
A.Rainer

Reputation: 719

Copy files with changing filename

I'm trying to copy several files with changing filenames. It seems very easy but I can't seem to work out how to do it without actually listing out the filenames in their entirety. The first few letters of the filenames correspond to the subject names which I'm looping through one by one. In each folder, there are 2 files, one is something like this subj1_load1_vs_load2.img, one is subj1_load1_vs_load2.hdr. I want both of them copied. Below is what I have:

subj={'subj1','subj2','subj3','subj4','subj5'}

for i=1:length(subj)
    source=fullfile(filedir,subj{i},sprintf('^%s_.*\.*',subj{i})); % this doesn't seem to work
    destination=fullfile(destdir,subj{i});
    copyfile(source,destination);
end

I've also tried:

source=dir([filedir subj{i} strcat(subj{i},'*')]);

This appears to needlessly complicate since I will need to deal with .name. But perhaps I don't know how to use this well.

Anyway, the problem is with source as I'm trying to find the files i want to copy.

I'd appreciate any suggestions.

Upvotes: 0

Views: 101

Answers (1)

A.Rainer
A.Rainer

Reputation: 719

Below is Daniel's answer (which solved the issue for me)

source=fullfile(filedir,subj{i},strcat(subj{i},'*'))

Upvotes: 1

Related Questions