exporting array data to text file

14 次查看(过去 30 天)
I have the following code which provides array of some random numbers:
if true
clc;
clear all;
close all;
files=dir('doc2.txt');
names=files.name;
fId=fopen(files.name,'r');
col=textscan(fId, '%f %f');
data=[col{:}];
time=data(:,1)';
freq=9*sqrt(data(:,2));
y=length(freq);
high(y)=freq(y);
for (i=1:(y-1))
high(i)=[freq(i)+freq(i+1)]/2;
end
low(1)=freq(1);
for (i=2:y)
low(i)=high(i-1);
end
n=20;
x=zeros(n,y);
for i=1:y
all=low(i):25000:high(i);
l=length(all);
if l<n
disp('Not possible to fit in 20 numbers!');
continue;
else
index=randperm(l,n);
end
x(:,i)=sort(all(index), 'ascend');
end
end
there is a variable 'all' which saves random number for the last loop. this code gives the output following: x =
1.0e+06 *
5.0000 6.0750 7.7500 8.7500 0 0
5.0500 6.1500 7.7750 8.7750 0 0
5.0750 6.1750 7.8250 8.8000 0 0
5.1250 6.2000 7.9500 8.8250 0 0
5.1750 6.3000 7.9750 8.8500 0 0
5.2750 6.3500 8.0250 8.8750 0 0
5.3000 6.4750 8.0750 8.9000 0 0
5.3250 6.5750 8.1500 8.9250 0 0
5.3500 6.7250 8.1750 8.9500 0 0
5.4000 6.7500 8.2500 8.9750 0 0
5.4250 6.8250 8.2750 9.0000 0 0
5.4750 7.0000 8.3250 9.0250 0 0
5.5000 7.0500 8.3500 9.0500 0 0
5.5250 7.2500 8.3750 9.0750 0 0
5.5500 7.3000 8.4500 9.1000 0 0
5.5750 7.3750 8.5250 9.1250 0 0
5.6000 7.4500 8.5750 9.1500 0 0
5.6250 7.5250 8.6250 9.1750 0 0
5.8000 7.5500 8.6500 9.2000 0 0
5.9000 7.5750 8.7000 9.2250 0 0
and the variable 'all' has following values:
ans =
1.0e+06 *
9.6500
9.6750
9.7000
9.7250
9.7500
9.7750
which is only for the range between 9.65M & 9.8M. but i want the output to be like the attached file. ultimately, the code will write a output file like 'final.txt'. (just assume 1.6*E6 is multiplied in the output values, no need to think about that)

回答(1 个)

Giridharan Kumaravelu
xTable = array2table(x);
writetable(xTable,'final.txt','Delimiter',' ');
The above code can help and you can modify the first row in the text editor. (You cannot have the column names as in the final.txt provided by you, as they are not valid variable names)

类别

Help CenterFile Exchange 中查找有关 Workspace Variables and MAT-Files 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by