Output data to text file

3 次查看(过去 30 天)
I have this code
H = 1;
h = 15;
l = 500;
g = 9.81;
n1 = 1;
n2 = 2;
n3 = 3;
L1 = (2*l)/n1;
L2 = (2*l)/n2;
L3 = (2*l)/n3;
T1 = (2*l)/(n1*sqrt(g*h));
T2 = (2*l)/(n2*sqrt(g*h));
T3 = (2*l)/(n3*sqrt(g*h));
t = 0;
x = 0:10:l;
k1 = (2*pi)/L1;
k2 = (2*pi)/L2;
k3 = (2*pi)/L3;
sigma1 = sqrt(g*k1*tanh(k1*h));
sigma2 = sqrt(g*k2*tanh(k2*h));
sigma3 = sqrt(g*k3*tanh(k3*h));
eta1 = (H/2)*cos(k1*x)*cos(sigma1*t);
eta2 = (H/2)*cos(k2*x)*cos(sigma2*t);
eta3 = (H/2)*cos(k3*x)*cos(sigma3*t);
figure
hold on
subplot(3,1,1);
plot(x,eta1)
title('n = 1');
subplot(3,1,2);
plot(x,eta2)
title('n = 2')
subplot(3,1,3);
plot(x,eta3)
title('n = 3');
I need to output the data into 3 text files with 2 columns where x = 1,2,3 for three modes respectfully. (n =1,2,3 are modes). How would I go about this?

采纳的回答

William Rose
William Rose 2021-9-28
On Matlab 2021, use writetable() or dlmwrite(). See help for each.
On Matlab 2018, use dlmwrite(), since writetable() was not yet in Matlab.
Use Matlab's aray capability to shorten your code.
I recommend that you rename variable l to xmax, since l and the numeral 1 look so similar in the Courier font used by Matlab.
Here is code that uses Matlab's array capability and creates the three files you requested.
H = 1;
h = 15;
xmax = 500;
g = 9.81;
n=1:3; %1x3 array
L=2*xmax./n; %1x3 array
T=2*xmax./(n*sqrt(g*h)); %1x3 array
t = 0;
x = 0:10:xmax; %1x51 array
k=2*pi./L; %1x3 array
sigma = sqrt(g*k.*tanh(k*h)); %1x3 array
eta=H/2*cos(k'*x).*cos(sigma'*t); %3x51 array
figure
for i=1:3
subplot(3,1,i);
plot(x,eta(i,:))
titlestr=sprintf('n=%d',i);
title(titlestr);
filename=sprintf('dataout%d.txt',i);
dlmwrite(filename,[x',eta(i,:)']);
end
Good luck with your work.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Large Files and Big Data 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by