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.
採用された回答
Simon Chan
2021 年 8 月 4 日
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).
Simon Chan
2021 年 8 月 4 日
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
2021 年 8 月 4 日
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.
Simon Chan
2021 年 8 月 4 日
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)
kimy
2021 年 8 月 5 日
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
2021 年 8 月 5 日
What MATLAB version are you using?
kimy
2021 年 8 月 5 日
R2018b, it requires to define a readcell function, right?
Simon Chan
2021 年 8 月 5 日
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
kimy
2021 年 8 月 5 日
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.
Simon Chan
2021 年 8 月 5 日
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
2021 年 8 月 5 日
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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
