MATLAB Answers

FileCopy does't work

1 view (last 30 days)
Jogger on 7 Sep 2019
Commented: Jogger on 18 Sep 2019
Copying files from source directory to destination directory is NOT working.
Below is my source code. Please help me
WorkingDir = 'C:\Users\shastry\MathWorks\test_that_results_hatch_B1CYKd';
OutputFolder = fullfile(WorkingDir, 'Tmp');
if ~exist(OutputFolder, 'dir')
Param.Source = WorkingDir; %'C:\Users...';
Param.Target = OutputFolder; %'C:\Users...';
Files = Files(~[Files.isdir]); % Remove folders from list;
for k = 1:length(Files)
if contains(Files(k).folder,'debug')
if contains(Files(k).name,'test_that')
CurrentFile = Files(k).name;
exist(CurrentFile,'file') % 1=exist 0=doesn't exist
RenameFile= regexprep(CurrentFile,{'\.'},{''}); % Replace dot from
exist(RenameFile,'file') % 1=exist 0=doesn't exist
copyfile(fullfile(Param.Source, CurrentFile), fullfile(Param.Target, RenameFile));% copy sourcefolder file to destnationfolder file
Guillaume on 8 Sep 2019
That looks much better, you're now using the actual folder where the file is found.
I assume your problem is solved?
I don't understand your comment about the . in the file name. Matlab IO functions don't care one bit about the format of the filename, you can have as many or as few . in the filename, it won't change anything.
Note that it would be clearer if you passed char arrays to to regexprep instead of scalar cell arrays:
RenameFile = regexprep(Files(k).name, '\.', '');
RenameFile = strrep(Files(k).name, '.', '');
and the whole lot, if it's needed would be better written only once before the ifs.
Also the double ifs could be written as just one:
if contains(Files(k).folder,'debug') && contains(Files(k).name,'test_that')

Sign in to comment.

Accepted Answer

Guillaume on 9 Sep 2019
I don't remember the details of your previous question, but clearly the code I gave you is meant to work with data imported a particular way. If you change the way you import the data, it's not going to work. Reading the code above Content must be a scalar structure where each field is itself a scalar structure with just one field which is a table.
In your mat file, Content is a cell array, not a structure as expected. So yes, you'll get an error but not the one you mention (it's fieldnames that would complain first). The cell array can trivially be converted into a structure but the substructures contain more than one table. As it is, the code would only use the first table. The code would have to be modified to cope with more than one table per structure, but it's unclear what you want to do.
Furthermore, the Content description you pasted above doesn't match your attached mat file. Above, Content is indeed a structure as expected, but the fields contain cell arrays, not structures. So, yes you'll get a struct2cell error from that.
I think I told you before, if the format of your input keep on changing don't expect the solutions you're given to work anymore. It's important to be consistant.
Jogger on 18 Sep 2019
Thanks a ton for all learnings i received from you!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!