files managing (copy, move and write)

2 views (last 30 days)
kimy
kimy on 4 Aug 2021
Edited: kimy on 6 Aug 2021
Hi,
Now I have a series of files but I don't know how to achieve my goal using Matlab since I am a definitely beginer. I have a folder called surface in which many folders are included, named by a time step (0.1, 0.2 ,0.3 .....). Of course, under each time step folder the data file (e.g. "0.1->patch_ground->scalarfiled->p") that I need is there, as you can recognise in the attchment, which is original data files that I output from an OPENFOAM code I have to write al data included in the file "p" to another file "p" with header under the folder of each timestep ("0.1->p"). By the same procedure, apply for each time step folder with a loop I think. I hope anybody can give me some tips, thanks a lot.

Accepted Answer

Simon Chan
Simon Chan on 4 Aug 2021
In your attached file, there is already a file 'p' inside folder '0.1' and I need to remove this file before running the following code. Otherwise, error would occur because copy or move a file onto itself is not allowed.
So please be aware to make sure all file with name 'p' only stored in the subdirectory 'C:\...\patch_ground\scalarField'.
listfolder = dir('**/p');
initial = {listfolder.folder};
destination = cellfun(@(x) strrep(x,'patch_ground\scalarField',''),initial,'UniformOutput',false);
cellfun(@(x,y) copyfile(fullfile(x,'p'),fullfile(y,'p')),initial,destination)
  13 Comments
kimy
kimy on 5 Aug 2021
Thanks, it works well. I am thinking that it is better to process the data file before combining header file. But I faild to modify it. I simply tried e.g. "newdata=data-1000".
kimy
kimy on 6 Aug 2021
Edited: kimy on 6 Aug 2021
I tried many times and I faild to arange the data inside "p" file. Could you give me some tips? Thanks.

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!