how to process the data in a file and repeat the procedure for different folders?

I have a set of files called "p" under the folder of "surface->0.1 (folder named by time steps)->patchgorund->scalarField". As you can see in the attachment, in the file "p" there are 400 data inside bracket. Now I have to change those data by minus a number, for example, p-1000. Then repeat this procedure for each folder (0.1, 0.2, 0.3.....->patchgorund->scalarField->p). Do you have any ideas?

 Accepted Answer

Here is an example for reading files and changing its content
q = 0.1:0.1:2;
folder = arrayfun(@(i)['surface\',num2str(q(i)),'\patch_ground\scalarField\p'],1:numel(q),'uniform',0);
for i = 1:1:numel(fid)
s = fileread(folder{i});
ch = s;
s1 = strfind(ch,'(');
s2 = strfind(ch,')');
data = str2num(ch(s1+1:s2-1)) - 1000; % minus 1000
ss = num2str(data','%f\n');
ch = [ch(1:s1), ss, ch(s2:end)];
fid = fopen(folder{i},'wt');
fprintf(fid,'%s',ch);
fclose(fid);
end

5 Comments

Thanks. I modified the code and now it works:
q = 0.1:0.1:2;
folder = arrayfun(@(i)[num2str(q(i)),'\patch_ground\scalarField\p'],1:numel(q),'uniform',0);
for i = 1:1:numel(folder)
s = fileread(folder{i});
ch = s;
s1 = strfind(ch,'(');
s2 = strfind(ch,')');
data = str2num(ch(s1+1:s2-1)) + 1000; % minus 1000
ss = num2str(data','%f\n');
ch = [ch(1:s1), ss, ch(s2:end)];
fid = fopen(folder{i},'wt');
fprintf(fid,'%s',ch);
fclose(fid);
end
I need to separate numer and bracket in separate line. Should I add '\n' this in the code? such as,
ch = [ch(1:s1),'\n', ss, '\n',ch(s2:end)];
If neccessary, i believe those you have modified, because you are the practitioner. For example, if the output file named 'p' conflicts with your OpenFoam software, then you need to use '\n', in order not to make mistakes.
However, what I got is like below if I added '\n', which seems to me '\n' was regarded as a string in this case.
400
(\n195910.263000
195908.263000
......
you can use newline instead ch = [ch(1:s1),newline, ss, newline,ch(s2:end)];

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Release

R2021a

Asked:

on 23 Aug 2021

Commented:

on 23 Aug 2021

Community Treasure Hunt

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

Start Hunting!