files managing (copy, move and write)

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.

 Respuesta aceptada

Simon Chan
Simon Chan el 4 de Ag. de 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 comentarios

kimy
kimy el 4 de Ag. de 2021
Editada: kimy el 4 de Ag. de 2021
Thanks for your anwser but the original "p" file cannot be removed since I need the header in this file. So my goal is to write the data inside "p" file (located in scalarField folder) in the "p" file (located in "0.1" folder).
Now I understand more about your needs.
The following code execute in the current working directory (myFolder), and the attached folders and files with name 'surface' is located inside the working directory as well.
On the other hand, the file name will change to p.txt (attached) instead of p in this case.
myFolder = 'C:\Users\'; % Working directory
destination = 'C:\Users\surface\0.1'; % Path for the "final p" file
movefile(fullfile(destination,'p'),myFolder); % Move p file to working folder
listfolder = dir('**/p'); % Retrieve all paths having p file
initial = {listfolder.folder};
data = cellfun(@readcell, fullfile(initial,'p'), 'UniformOutput', false); % Read the information inside p file
alldata = cat(1,data{:}); % Combine data from all the p file
header = readcell('p'); % Read the header from the "final p" file
writecell([header;alldata],'p.txt'); % Write to another file with name p.txt
movefile('p.txt',destination); % Put it back to the required directory
kimy
kimy el 4 de Ag. de 2021
Sorry, I did not make you clear. "p" must be put in each separate time step folder, instead of "0.1" folder. I attached the target files that I want but the folder named starting by "40". I moved them one by one manually.
Suggestions:
(1) p file only stored in folder C:\...\patch_ground\scalarField, do not put them manually inside the time step folder.
(2) Use the file originally located inside folder 0.1 as the header file, rename it as 'header' and put inside the working directory.
The code below would retrieve data from the p file (without header), combine with the header, write it to the time step folder and in this case rename it back to 'p' as well
listfolder = dir('**/p');
original_location = {listfolder.folder};
destination = cellfun(@(x) strrep(x,'patch_ground\scalarField',''),original_location,'UniformOutput',false);
data = cellfun(@readcell, fullfile(original_location,'p'), 'UniformOutput', false);
header = readcell('header');
cellfun(@(x,y) writecell([header;x],fullfile(y,'p.txt')),data,destination);
cellfun(@(x) movefile(fullfile(x,'p.txt'),fullfile(x,'p')),destination)
Thanks a lot. The following error appearred:
>> importfinale
Error using cellfun
Undefined function or variable 'readcell'.
Error in importfinale (line 4)
data = cellfun(@readcell, fullfile(original_location,'p'), 'UniformOutput', false);
Simon Chan
Simon Chan el 5 de Ag. de 2021
What MATLAB version are you using?
kimy
kimy el 5 de Ag. de 2021
R2018b, it requires to define a readcell function, right?
Simon Chan
Simon Chan el 5 de Ag. de 2021
function readcell was introduced from R2019a, so you may need some other import function.
kimy
kimy el 5 de Ag. de 2021
Editada: kimy el 5 de Ag. de 2021
I updated matlab and the script works. Thanks.
In fact the original header file only includes the characters without data. So the script did not work if I removed the data in the header file, indicating an error that the dimension is not consistent.
/*--------------------------------*- C++ -*----------------------------------*\
| ========= | |
| \\ / F ield | foam-extend: Open Source CFD |
| \\ / O peration | Version: 4.1 |
| \\ / A nd | Web: http://www.foam-extend.org |
| \\/ M anipulation | |
\*---------------------------------------------------------------------------*/
FoamFile
{
version 2.0;
format ascii;
class scalarAverageField;
object values;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Average
0
// Data on points
kimy
kimy el 5 de Ag. de 2021
Excuse me, I forgot to mention that before writing, the data in each "p" file should be divided a number. However I could not adress it so far. Sorry for the bad explanation.
I try to remove the point data from the file and import it via function readcell, the result has several columns instead of one. This may be the reason why the error occurs.
Try to replace the following line:
header = readcell('header');
with this:
header = readcell('header','Delimiter','\n');
The result shoule be in one column only:
18×1 cell array
{'/*--------------------------------*- C++ -*----------------------------------*\'}
{'| ========= | |'}
{'| \\ / F ield | foam-extend: Open Source CFD |'}
{'| \\ / O peration | Version: 4.1 |'}
{'| \\ / A nd | Web: http://www.foam-extend.org |'}
{'| \\/ M anipulation | |'}
{'\*---------------------------------------------------------------------------*/'}
{'FoamFile' }
{'{' }
{'version 2.0;' }
{'format ascii;' }
{'class scalarAverageField;' }
{'object values;' }
{'}' }
{'// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //'}
{'// Average' }
{[ 0]}
{'// Data on points' }
For any other processing, you can access the variable 'data' which stores all point data from the p file. So you can do whatever you like before combine with the header and write the file.
kimy
kimy el 5 de Ag. de 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 el 6 de Ag. de 2021
Editada: kimy el 6 de Ag. de 2021
I tried many times and I faild to arange the data inside "p" file. Could you give me some tips? Thanks.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre File Operations en Centro de ayuda y File Exchange.

Preguntada:

el 4 de Ag. de 2021

Editada:

el 6 de Ag. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by