MATLAB Answers

# how to output data into a text file

2 views (last 30 days)
Nicholas Deosaran on 24 Sep 2020
Answered: Jalaj Gambhir on 28 Sep 2020
Hello everyone,
I am having an issue with how to export data to a text file.
below is my code that I have and would like to export it to 3 different text files.
clc
clear
% Given basin parameters
g = 9.81; % gravity
h = 15; % water depth
l = 500; % basin length
n = 1; % node
% Function to get wave length
L1 = 2*l/n;
disp('At node = 1');
disp(['Wavelength L = ' num2str(L1)]);
% Function to get wave period
T1 = (2*l)/(n*sqrt(g*h));
disp(['Wave Period at T = ' num2str(T1)]);
% Given basin parameters
g = 9.81; % gravity
h = 15; % water depth
l = 500; % basin length
a = 2; % node
L2 = 2*l/a;
disp('At node = 2');
disp(['Wavelength L = ' num2str(L2)]);
% Function to get wave period
T2 = (2*l)/(a*sqrt(g*h));
disp(['Wave Period at T = ' num2str(T2)]);
% Given basin parameters
g = 9.81; % gravity
h = 15; % water depth
l = 500; % basin length
b = 3; % node
L3 = 2*l/b;
disp('At node = 3');
disp(['Wavelength L = ' num2str(L3)]);
% Function to get wave period
T3 = (2*l)/(b*sqrt(g*h));
disp(['Wave Period at T = ' num2str(T3)]);
%information needed for free surface profile
t=0;
H = 1; % wave height
x = 0:l;
k1 = 2*pi/L1;
k2 = 2*pi/L2;
k3 = 2*pi/L3;
sigma1 = 2*pi/T1;
sigma2 = 2*pi/T2;
sigma3 = 2*pi/T3;
N1 = H/2*cos(k1*x)*cos(sigma1*t);
N2 = H/2*cos(k1*x)*cos(sigma2*t);
N3 = H/2*cos(k1*x)*cos(sigma3*t);
subplot(3,1,1);
N1 = H/2*cos(k1*x)*cos(sigma1*t);
plot(x,N1, 'r')
hold on
yline(0);
ylim([-1 1])
title ('mode n = 1, L = l/2')
xlabel ('x(m)')
ylabel ('eta (m)')
legend('H/2*cos(k1*x)*cos(sigma2*t)')
subplot(3,1,2)
N2 = H/2*cos(k2*x)*cos(sigma2*t);
plot (x,N2,'r');
hold on
yline(0);
ylim([-1 1])
title ('mode n = 2, L = l')
xlabel ('x(m)')
ylabel ('eta (m)')
legend('H/2*cos(k2*x)*cos(sigma2*t)')
subplot(3,1,3)
N3 = H/2*cos(k3*x)*cos(sigma3*t);
plot(x, N3, 'r')
hold on
yline(0);
ylim([-1 1])
title ('mode n = 3, L= 2l/3')
xlabel ('x(m)')
ylabel ('eta (m)')
legend('H/2*cos(k3*x)*cos(sigma2*t)')
Thank you for your help

#### 0 Comments

Sign in to comment.

### Accepted Answer

Jalaj Gambhir on 28 Sep 2020
Hi,
As it is not very obvious what exactly you would like to write in your text file, I am assuming you want to write the statements you have used inside disp function in the text file.
To do so, you can use fprintf, you can find a helpful solution here.
Hope this helps.

#### 0 Comments

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!