modify a data file using MATLAB?
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
Réponse acceptée
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
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur File Operations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!