Hi guys,
I am a beginner of MATLAB. Now I have a series of data files output from Openfoam, in terms of different time step (shown in floowing figure). A
and under each folder I have the data included in the file like below. However I only need the column of x and p for each file.
I would like to integrate the files in one separate file and add a second colum between x and p, by t (t=0.5, 1, 1.5 ....)., shown in following format. I have no experience of modyfing a file. Could you please give me some tips, thanks a lot.
%x t value
0 0.5 3000
1 0.5 3200
...
20 30 3690

댓글 수: 2

Mathieu NOE
Mathieu NOE 2021년 6월 28일
hello
could you share some of the data file ?
tx
kimy
kimy 2021년 6월 28일
for sure. Thanks for your reply.
Original files are sampleDict.zip whereas the p_gorund.csv is what I want (this file was made manually)

댓글을 달려면 로그인하십시오.

 채택된 답변

Mathieu NOE
Mathieu NOE 2021년 6월 28일

0 개 추천

so this is it !
hope it helps
see the attached file too - you will need them to have the folders sorted in natural order , which is not what matlab does "naturally" , so to say
main code :
clc
clearvars
S = dir('**/*.raw');
[m,n] = size(S);
%% first we have to sort the folders in natural order - otherwise the time steps
% will not be uniformly increasing
for ci = 1:m
folders{ci} = S(ci).folder;
filenames{ci} = S(ci).name;
end
[folders_sort,ndx,dbg] = natsort(folders);
filenames_sort = filenames(ndx);
%% main loop for data extraction and concatenation
outdata = [];
for k = 1:m
foldername = char(folders_sort(k)) % display in command window the folder name
filename = char(filenames_sort(k)) % display in command window the file name
if strcmp(foldername(end-1:end),'\0')
% do nothing (skip t = 0 results)
else
F = fullfile(foldername, filename);
data = importdata(F, ' ', 2);
data = data.data;
[m,n] = size(data);
% get the simulation time step value from folder name
ind = strfind(foldername,'\');
time_step = str2num(foldername(ind(end)+1:end));
tmp = [data(:,1) time_step*ones(m,1) data(:,4)];
% now vertical concatenation of all files data
outdata = [outdata ; tmp];
end
end
%% export data
T = array2table(outdata);
T.Properties.VariableNames(1:3) = {'%x','t','p'} % %x t p
writetable(T,'file1.csv')

댓글 수: 7

kimy
kimy 2021년 6월 28일
Thanks a lot.
Mathieu NOE
Mathieu NOE 2021년 6월 28일
You're welcome
kimy
kimy 2021년 7월 15일
Hi, I met a new problem. Could you please help me to have a look?
Mathieu NOE
Mathieu NOE 2021년 7월 15일
yes - what's the issue ?
Rik
Rik 2021년 7월 15일
Probably this one?
kimy
kimy 2021년 7월 15일
편집: kimy 2021년 7월 15일
yes, I have two types of data output (sampleDict and toppressure), but I need rearrange them like the format shown in "finaldata" based on any one of two data file..
kimy
kimy 2021년 7월 19일
Anyone has ideas?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Cell Arrays에 대해 자세히 알아보기

태그

질문:

2021년 6월 28일

댓글:

2021년 7월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by