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.
采纳的回答
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 个评论
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
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);
What MATLAB version are you using?
R2018b, it requires to define a readcell function, right?
function readcell was introduced from R2019a, so you may need some other import function.
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
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.
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".
I tried many times and I faild to arange the data inside "p" file. Could you give me some tips? Thanks.
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 File Operations 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
