Effacer les filtres
Effacer les filtres

modify a data file using MATLAB?

9 vues (au cours des 30 derniers jours)
kimy
kimy le 28 Juin 2021
Commenté : kimy le 19 Juil 2021
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 commentaires
Mathieu NOE
Mathieu NOE le 28 Juin 2021
hello
could you share some of the data file ?
tx
kimy
kimy le 28 Juin 2021
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)

Connectez-vous pour commenter.

Réponse acceptée

Mathieu NOE
Mathieu NOE le 28 Juin 2021
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 commentaires
kimy
kimy le 15 Juil 2021
Modifié(e) : kimy le 15 Juil 2021
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 le 19 Juil 2021
Anyone has ideas?

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur File Operations dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by