multiple text files into one in a for loop

4 views (last 30 days)
Hi,
I am trying to join multiple text files into one. RIght now, in my code, I have IT number of datafiles but I would like to all combine into one with the rows following on from the previous iteration. The file needs to contain all the columns of data that are shown in the table P to store large amounts of data. Here is my code:
If anyone can help it would be greatly appreciated.
for IT = 1:50
x1 = -15+30*rand(3,1);
x2 = -15+30*rand(3,1);
N = 2;
r_t = 7000*1000; %km
g_t = 9.8; %m/s2
r = r_t;
g = g_t;
tau_d = 300;
t = 1;
omega = sqrt(g/r);
% target positions
e1 = [1; 1; 0];
e2 = [0; 0; -1];
%parameters and unknowns
mu = 398600.5;
n = sqrt(mu/r_t^3);
b = 0.112;
k1 = 1;
d = 0.037;
k2 = 1;
norm_d = norm(e1-e2);
c = -d/norm_d * exp(-norm_d^2/k2) - b/norm_d*exp(-norm_d^2/k1);
lambda = [b, c, d, k1, k2];
v1_gf = gf_sat_1_vel2(x1, x2, e1, e2, lambda, omega, tau_d, t, N);
v2_gf = gf_sat_1_vel2(x1, x2, e1, e2, lambda, omega, tau_d, t, N);
v10 = [0; 0; 0];
v20 = [0; 0; 0];
y0 = [x1(1); x1(2); x1(3); v10(1); v10(2); v10(3); x2(1); x2(2); x2(3); v20(1); v20(2); v20(3)];
dt=1;
tspan = 0:dt:1000;
[t,y] = ode45(@(t,y)ClohesseyWiltshire(y, n, lambda, e1, e2, omega, tau_d, t, N),tspan, y0);
U1 = zeros(length(t),length(e1));
U2 = zeros(length(t),length(e2));
dv1 = 0;
dv2 = 0;
for i = 1 : length(t)
[u1, u2, vg1, vg2] = ClohesseyWiltshire_u(y(i,:), n, lambda, e1, e2, omega, tau_d, t(i), N);
U1(i, :) = u1;
U2(i, :) = u2;
VD1(i, :) = vg1;
VD2(i, :) = vg2;
U1_norm(i) = norm(u1);
U2_norm(i) = norm(u2);
v1_gf = gf_sat_1_vel2(x1, x2, e1, e2, lambda, omega, tau_d, t(i), N);
V1GF(i, :) = v1_gf;
v2_gf = gf_sat_2_vel2(x1, x2, e1, e2, lambda, omega, tau_d, t(i), N);
V2GF(i, :) = v2_gf;
if i ~= 1
dv1 = norm(u1) * (t(i) - t(i-1)) + dv1;
dv2 = norm(u2) * (t(i) - t(i-1)) + dv2;
end
end
R1 = y(:,1:3);
R1 = R1';
V1 = y(:,4:6);
V1 = V1';
R2 = y(:,7:9);
R2 = R2';
V2 = y(:,10:12);
V2 = V2';
title('Trajectories of Two Satellite Configuration - Gravitational Field')
xlabel('X')
ylabel('Y')
zlabel('Z')
legend({'Satellite 1','Satellite 2'},'Location','Southwest')
V1 = V1';
V2 = V2';
R1 = R1';
R2 = R2';
U1_norm = U1_norm';
U2_norm = U2_norm';
tspan = tspan';
% Create columns of data
% Create a table with the data and variable names
P = table(t, U1, VD1, U2, VD2, V1, V2, R1, R2, V1GF, V2GF, 'VariableNames', {'t', 'U1', 'VD1', 'U2', 'VD2' 'V1', 'V2', 'R1', 'R2', 'V1GF', 'V2GF'});
% Write data to text file
name = ['Database2/',num2str(IT), 'Database.txt'];
writetable(P, name)
end
  3 Comments
Chris Murchison
Chris Murchison on 24 Feb 2021
Thanks for the reply, how would I do this?
Rik
Rik on 24 Feb 2021
I never work with tables, so I would have to do the same thing as you: either look through the documentation, or try [P1;P2], or see if it is easier to read the files and merge the text output.

Sign in to comment.

Answers (1)

Divya Gaddipati
Divya Gaddipati on 12 Apr 2021
Initialize a table before the loop
You can combine two tables inside the loop in the following way:
T = table(); % Create table with the required variable names
for i=1:IT
% Create table inside the loop to append to the original
Tnew = table();
T = [T; Tnew];
end

Categories

Find more on Data Import and Export in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!